【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“MobileGunCamera.java”一文,供大家参考学习!
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
public class MobileGunCamera
{
// initial camera position
private static final float X_POS = 0.0f;
private static final float Y_POS = 0.4f;
private static final float Z_POS = 2.0f;
// translation and rotation increments
private static final float ANGLE_INCR = 3.0f; // degrees
private static final float MOVE_INCR = 0.1f;
// booleans for remembering key presses
private boolean upPressed = false;
private boolean downPressed = false;
private boolean leftPressed = false;
private boolean rightPressed = false;
// for examining the camera's (x,y,z) position
private Transform trans = new Transform();
private float[] transMat = new float[16];
private float xCoord, yCoord, zCoord;
// for storing the camera's current y- rotations
private float yAngle;
private Transform rotTrans = new Transform();
// scene graph elements for the camera
private Group transGroup;
private Camera cam;
private Mesh shotMesh;
public MobileGunCamera(int width, int height)
/* Build the camera scene graph branch:
transGroup --> camera
|---> gun image mesh
|---> shot image mesh
*/
{
cam = new Camera();
float aspectRatio = ((float) width) / ((float) height);
cam.setPerspective(70.0f, aspectRatio, 0.1f, 50.0f);
cam.postRotate(-2.0f, 1.0f, 0, 0); // apply x-axis rotations to camera
// gun image mesh
Image2D imGun = loadImage("/gun.png");
ImageSquare imSqGun = new ImageSquare(imGun, 0.025f, -0.055f, -0.1f, 0.05f);
Mesh gunMesh = imSqGun.getImageSquareMesh();
gunMesh.setPickingEnable(false);
// shot image mesh
Image2D imShot = loadImage("/shot.png");
ImageSquare imSqShot =
new ImageSquare(imShot, 0, -0.05f, -0.15f, 0.1f);
shotMesh = imSqShot.getImageSquareMesh();
shotMesh.setPickingEnable(false);
shotMesh.setRenderingEnable(false); // invisible intially
// put eveything together
// transGroup handles both camera translation and y-axis rotations
transGroup = new Group(); // no initial rotation
trans.postTranslate(X_POS, Y_POS, Z_POS);
transGroup.setTransform(trans);
transGroup.addChild(cam);
transGroup.addChild(gunMesh);
transGroup.addChild(shotMesh);
// store initial rotation
yAngle = 0.0f;
} // end of MobileGunCamera()
public Group getCameraGroup()
{ return transGroup; }
public Camera getCamera()
{ return cam; }
private Image2D loadImage(String fn)
// load the image stored in fn
{ Image2D im = null;
try {
im = (Image2D)Loader.load(fn)[0];
}
catch (Exception e)
{ System.out.println("Cannot make image from " + fn); }
return im;
} // end of loadImage()
// ----- handle key presses / releases, and return details ------
public void pressedKey(int gameAction)
{
switch(gameAction) {
case Canvas.UP: upPressed = true; break;
case Canvas.DOWN: downPressed = true; break;
case Canvas.LEFT: leftPressed = true; break;
case Canvas.RIGHT: rightPressed = true; break;
default: break;
}
} // end of pressedKey()
public void releasedKey(int gameAction)
{
switch(gameAction) {
case Canvas.UP: upPressed = false; break;
case Canvas.DOWN: downPressed = false; break;
case Canvas.LEFT: leftPressed = false; break;
case Canvas.RIGHT: rightPressed = false; break;
default: break;
}
} // end of releasedKey()
// ------------ position and direction access methods ------------
public float[] getPosition()
// return the current position of the camera
{ storePosition();
return new float[] { xCoord, yCoord, zCoord };
} // end of getPosition()
private void storePosition()
// extract the current (x,y,z) position from transGroup
{
transGroup.getCompositeTransform(trans);
trans.get(transMat);
xCoord = transMat[3];
yCoord = transMat[7];
zCoord = transMat[11];
} // end of storePosition()
public float[] getDirection()
/* Rotate the vector (0,0,-1) by the current y-axis rotation.
(0,0,-1) represents the initial forward direction for the
camera. The result after the rotation will be the camera's
current forward direction.
*/
{ float[] zVec = {0, 0, -1.0f, 0};
rotTrans.transform(zVec);
return new float[] { zVec[0], zVec[1], zVec[2] };
} // end of getDirection()
// ------------- update camera position and rotation ---------------
public void update()
{
if (upPressed || downPressed)
updateMove();
else if (leftPressed || rightPressed)
updateRotation();
} // end of update()
private void updateMove()
{
transGroup.getTransform(trans);
if (upPressed) // move forward
trans.postTra