我正在libgdx制作赛车游戏,当两辆车左右碰撞然后无法正确拉过来时,我的英雄车类代码是...
public class HeroCar extends DynamicGameObject{
public static final int HERO_STATE_RUN = 0;
public static final int HERO_STATE_HIT = 1;
public static final int HERO_STATE_FASTRUN = 2;
public static final float HERO_RUN_VELOCITY = 11.5f;
public static final float HERO_MOVE_VELOCITY = 10.0f;
public static final float HERO_STATE_FASTRUN_VELOCITY = 21.0f;
public static float HERO_WIDTH = 0.8f;
public static float HERO_HEIGHT = 1.6f;
int state;
float stateTime = 0;
public float heroCarScore;
public HeroCar(float x, float y) {
super(x, y, HERO_WIDTH, HERO_HEIGHT);
hitCount = 0;
fireCount = 10;
state = HERO_STATE_RUN;
heroCarScore = 0;
}
public void update(float deltaTime){
if(state != HERO_STATE_HIT){
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
bounds.x = position.x - bounds.width / 2;
bounds.y = position.y - bounds.height / 2;
if (position.x < 2.25f) position.x = 2.25f;
if (position.x > World.WORLD_WIDTH - 3.1f) position.x = World.WORLD_WIDTH - 3.1f;
}
else{
if(stateTime >= HERO_EXPLOSION_TIME){
stateTime = 0;
state = HERO_STATE_RUN;
}
else
state = HERO_STATE_HIT;
}
stateTime += deltaTime;
}
public void hitEnemyCar(){
state = HERO_STATE_HIT;
hitCount++;
stateTime = 0;
velocity.y = 0;
}
}
我的敌人的汽车类代码是
public class EnemyCars extends DynamicGameObject{
public static final int ENEMYCAR_STATE_RUN = 0;
public static final int ENEMYCAR_STATE_DEAD = 1;
public static final int ENEMYCAR_STATE_CROSSES = 2;
public static final float ENEMYCAR_RUN_VELOCITY = 8.0f;
public static final float ENEMYCAR_MOVE_VELOCITY = 0.8f;
public static float ENEMYCAR_WIDTH = 0.9f;
public static float ENEMYCAR_HEIGHT = 2.0f;
public int enemyCarState;
float stateTime = 0;
public int carType;
public EnemyCars(float x, float y, int carType) {
super(x, y, ENEMYCAR_WIDTH, ENEMYCAR_HEIGHT);
enemyCarState = ENEMYCAR_STATE_RUN;
velocity.set(0, ENEMYCAR_RUN_VELOCITY);
this.carType = carType;
}
public void update(float deltaTime){
if(enemyCarState != ENEMYCAR_STATE_DEAD)
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
bounds.x = position.x - bounds.width / 2;
bounds.y = position.y - bounds.height / 2;
if (position.x < 2.8f){
position.x = 2.8f;
velocity.x = ENEMYCAR_MOVE_VELOCITY;
}
if (position.x > World.WORLD_WIDTH - 3.4f){
position.x = World.WORLD_WIDTH - 3.4f;
velocity.x = -ENEMYCAR_MOVE_VELOCITY;
}
stateTime += deltaTime;
}
}
我的碰撞代码是
private void checkEnemyCarCollisions() {
if(heroCar.state == HeroCar.HERO_STATE_HIT)
return;
int len = enemyCars.size();
for(int i = 0; i < len; i++){
EnemyCars enemyCar = enemyCars.get(i);
if(OverlapTester.overlapRectangles( heroCar.bounds, enemyCar.bounds) && enemyCar.enemyCarState != EnemyCars.ENEMYCAR_STATE_DEAD){
heroCar.hitEnemyCar();
if(Settings.vibrateEnabled){
Gdx.input.vibrate(500);
}
return;
}
}
}
和OverlapTester类overlapRectangles方法是
public static boolean overlapRectangles (Rectangle r1, Rectangle r2) { //(bob bounds,platform bounds)
//System.out.println("OverlapTester class overlapRectangles method call");
if (r1.x < r2.x + r2.width && r1.x + r1.width > r2.x && r1.y < r2.y + r2.height && r1.y + r1.height > r2.y)
return true;
else
return false;
}