📄 car.java
字号:
package game.entities;
import javax.microedition.m3g.*;
import game.terrain.*;
public class Car
{
static Group carGroup;
public CarCanvas parent;
public Velocity velocity = new Velocity();
public float [] carCoords = new float[3];
public static float matrix[]=new float[16];
private static final float X_POS = 0.0f;
private static final float Y_POS = 0.01f;
private static final float Z_POS = 0.0f;
private static float ANGLE_INCR = 2.0f;
public static float MOVE_INCR = 0.0f;
public boolean upPressed = false;
public boolean downPressed = false;
public boolean leftPressed = false;
public boolean rightPressed = false;
private Transform trans = new Transform();
private float[] transMat = new float[16];
public float xCoord, yCoord, zCoord;
public float xAngle = 0;
public float yAngle;
public float zAngle = 0;
float lol = 0;
private Group transGroup = new Group();
private Group group = new Group();
private Camera backcam = new Camera();
private Camera nosecam = new Camera();
private Camera sidecam = new Camera();
public int width;
public int height;
Group wheel1;
Group wheel2;
Group wheel3;
Group wheel4;
Transform tr_cam = new Transform();
private boolean up_cam = false;
private boolean down_cam = false;
static final int UP_CAM = 9;
static final int DOWN_CAM = 11;
static final int SHOW_POSITION = 10;
static final int UP = 1; //Canvas.UP = UP
static final int DOWN = 6; //Canvas.DOWN = DOWN
static final int LEFT = 2; //Canvas.LEFT = LEFT
static final int RIGHT = 5;//Canvas.RIGHT = RIGHT
private MobileBackground mobileBackGnd;
private Group floorGroup ;
private RayIntersection ri = new RayIntersection();
private final float MAX_HEIGHT =100f;
public void up_cam()
{
backcam.getTransform(tr_cam);
tr_cam.postTranslate(0,0.05f,0);
backcam.setTransform(tr_cam);
}
public void down_cam()
{
backcam.getTransform(tr_cam);
tr_cam.postTranslate(0,-0.05f,0);
backcam.setTransform(tr_cam);
}
//构造函数
public Car(int w, int h)
{
width = w;
height = h;
set();
}
private Group scaleCar = new Group();
public void set()
{
setCamera();
setCarEntity();
transGroup = new Group();
trans.postTranslate(X_POS, Y_POS, Z_POS);
transGroup.addChild(backcam);
transGroup.addChild(nosecam);
transGroup.addChild(sidecam);
scaleCar.addChild(carGroup);
scaleCar.scale(0.5f, 0.5f, 0.5f);
transGroup.addChild(scaleCar);
transGroup.setTransform(trans);
yAngle = 0.0f;
wheel1 = (Group) carGroup.find(65);
wheel2 = (Group) carGroup.find(69);
wheel3 = (Group) carGroup.find(63);
wheel4 = (Group) carGroup.find(67);
}
public void setCamera()
{
float aspectRatio = ((float) width) / ((float) height);
backcam.postRotate(-20,1,0,0);
backcam.setTranslation(0,2,4);;
backcam.setPerspective(60.0f, aspectRatio, 0.5f, 1000.0f);
nosecam.setPerspective(60.0f,aspectRatio,0.5f,1000);
nosecam.setTranslation(0,0.75f,0.25f);
sidecam.setPerspective(60.0f,aspectRatio,0.5f,1000);
sidecam.setTranslation(-2,0.75f,2.5f);
sidecam.postRotate(-45,0,1,0);
}
public void setCarEntity()
{
World carw = loadWorld("/models/car.m3g");
carGroup = (Group) carw.find(62);
carw.removeChild(carGroup);
}
//载入World
public World loadWorld(String fn)
{
Object3D[] tile = load(fn);
World w = null;
for (int i = 0; i < tile.length; i++){
try{
w = (World)tile[i];
break;
}catch(Exception ex){
}
}
return w;
}
//载入3DObject
public Object3D[] load( String fn ){
Object3D[] obj = null;
try {
obj = Loader.load(fn);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public Group getCameraGroup()
{
return transGroup;
}
public Group getObjectGroup()
{
return group;
}
//获取照相机
public Camera getBackCamera()
{
return backcam;
}
public Camera getNoseCamera() {
return nosecam;
}
public Camera getSideCamera() {
return sidecam;
}
//响应键盘(按下)
public void pressedKey(int gameAction){
switch(gameAction) {
case UP_CAM: up_cam = true; break;
case DOWN_CAM: down_cam = true; break;
case UP: upPressed = true; break;
case DOWN: downPressed = true; break;
case LEFT: leftPressed = true; break;
case RIGHT: rightPressed = true; break;
default: break;
}
}
//响应键盘(释放)
public void releasedKey(int gameAction){
switch(gameAction) {
case UP_CAM: up_cam = false; break;
case DOWN_CAM: down_cam = false; break;
case UP: upPressed = false; break;
case DOWN: downPressed = false; break;
case LEFT: leftPressed = false; break;
case RIGHT: rightPressed = false; break;
default: break;
}
}
//响应键盘,移动汽车
public void update(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if( up_cam )
{
up_cam();
}
if( down_cam )
{
down_cam();
}
transGroup.getTransform(trans);
getPosition();
if (upPressed){
if (MOVE_INCR >= 0)
MOVE_INCR = velocity.accelerate();
else if (MOVE_INCR < 0)
MOVE_INCR = -velocity.brake(20);
}
else if (downPressed){
if (MOVE_INCR <= 0){
MOVE_INCR = -velocity.accelerate();
}else if (MOVE_INCR > 0){
MOVE_INCR = velocity.brake();
}
}else{
if (MOVE_INCR >= 0)
MOVE_INCR = velocity.brake(60);
else
MOVE_INCR = -velocity.brake(60);
}
if (MOVE_INCR != 0)
{
if(leftPressed || rightPressed)
updateYaw();
}
updateMove();
backcam.setTranslation(lol,2.5f,4);
float f[] = new float[4];
wheel2.getOrientation(f);
if(leftPressed){
if (lol > -1)
lol = lol - 0.1f;
if (f[0] < 140) {
wheel1.postRotate( -5, 0, 0, 1);
wheel2.postRotate( -5, 0, 0, 1);
}
}else if(rightPressed){
if (lol < 1)
lol = lol + 0.1f;
if (f[0] > 100) {
wheel1.postRotate(5, 0, 0, 1);
wheel2.postRotate(5, 0, 0, 1);
}
}else{
if (lol < -0.1f)
lol = lol + 0.1f;
else if (lol > 0.1f)
lol = lol - 0.1f;
else
lol = 0;
if (f[0] > 119.99975f) {
wheel1.postRotate(5, 0, 0, 1);
wheel2.postRotate(5, 0, 0, 1);
}else if (f[0] < 119.99975f) {
wheel1.postRotate( -5, 0, 0, 1);
wheel2.postRotate( -5, 0, 0, 1);
}
}
wheel1.getChild(0).postRotate(-MOVE_INCR * 100,0,1,0);
wheel2.getChild(0).postRotate(-MOVE_INCR * 100,0,1,0);
wheel3.getChild(0).postRotate(-MOVE_INCR * 100,0,1,0);
wheel4.getChild(0).postRotate(-MOVE_INCR * 100,0,1,0);
terrainCheck();
}
//更新汽车的移动位置
public void updateMove(){
trans.postTranslate(0, 0, -MOVE_INCR);
}
//更新汽车的偏移角
public void updateYaw(){
if (leftPressed) {
yAngle += ANGLE_INCR;
}else if (rightPressed){
yAngle -= ANGLE_INCR;
}
if (yAngle >= 360.0f)
yAngle -= 360.0f;
else if (yAngle < -360.0f)
yAngle += 360.0f;
storePosition();
trans.setIdentity();
trans.postTranslate(xCoord, yCoord, zCoord);
trans.postRotate(yAngle, 0, 1.0f, 0);
if( rightPressed )
{
mobileBackGnd.moveLeft(1);
}else{
mobileBackGnd.moveRight(1);
}
}
//获得汽车的位置坐标
public float[] getPosition(){
storePosition();
return new float[] { xCoord, yCoord, zCoord };
}
//存储汽车的位置坐标
private void storePosition(){
transGroup.getCompositeTransform(trans);
trans.get(transMat);
xCoord = transMat[3];
yCoord = transMat[7];
zCoord = transMat[11];
}
public float[] getAngle()
{
return new float[] {xAngle,yAngle,zAngle};
}
//得到汽车的偏移角(和z轴负方向之间)
public float getYAngle()
{
return yAngle;
}
public String toString() {
return super.toString();
}
public void addCollisionGroup(Group _collisionGroup) {
floorGroup = _collisionGroup;
}
public void setParent(CarCanvas top)
{
parent = top;
}
//地形跟随
private void terrainCheck()
{
//transGroup.setTransform(trans);需要连着调用两次,
//第一次调用前,得到了汽车下次的坐标,
//第二次调用前,进行汽车Y坐标的调整
transGroup.setTransform(trans);
storePosition();
//如发现与地形MESH相交,则进行地形跟随
if ( floorGroup.pick(-1,
xCoord,
MAX_HEIGHT,
zCoord,
0,
-1,
0,
ri) )
{
yCoord = MAX_HEIGHT - ri.getDistance();
transMat[7]=yCoord;
trans.set(transMat);
}
transGroup.setTransform(trans);
}
// 背景只平移,不上下移动
public void addBackGround(MobileBackground mb) {
mobileBackGnd = mb ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -