I am working on a simple tank game, move around in tank and shoot stuff. I based my move rotation on the car example (the tilting based on ground), but I am having problems. The tank moves correctly for a while on the hills i made, but it soon rotates out of control and flies upward. Any help?
public boolean place(Object3D ground) {
//get bottom direction
Object3D o = new Object3D(3);
o.addTriangle(getFrontTireRight().getTransformedCenter(),
getBackTireLeft().getTransformedCenter(),
getFrontTireLeft().getTransformedCenter());
o.calcNormals();
SimpleVector v = o.getPolygonManager().getTransformedNormal(0);
float dis = 300f;
boolean good = false;
Object3D body = getBody();
//move up in case of vertical ascending
body.translate(0,-150,0);
float disFrontLeftBottom = getTireLeft().rayIntersectsAABB(
getFrontTireLeft().getTransformedCenter(),v,false);
float disFrontRightBottom = getTireRight().rayIntersectsAABB(
getFrontTireRight().getTransformedCenter(),v,false);
float disBackLeftBottom = getTireLeft().rayIntersectsAABB(
getBackTireLeft().getTransformedCenter(),v,false);
float disBackRightBottom = getTireRight().rayIntersectsAABB(
getBackTireRight().getTransformedCenter(),v,false);
float disCenterBottom = getTireLeft().rayIntersectsAABB(
getTireInsideLeft().getTransformedCenter(),v,false);
SimpleVector vFrontLeft = new SimpleVector(
getFrontTireLeft().getTransformedCenter());
SimpleVector vFrontRight = new SimpleVector(
getFrontTireRight().getTransformedCenter());
SimpleVector vBackLeft = new SimpleVector(
getBackTireLeft().getTransformedCenter());
SimpleVector vBackRight = new SimpleVector(
getBackTireRight().getTransformedCenter());
SimpleVector vBody = new SimpleVector(
getBody().getTransformedCenter());
vFrontLeft.add(new SimpleVector(0,-disFrontLeftBottom,0));
vFrontRight.add(new SimpleVector(0,-disFrontRightBottom,0));
vBackLeft.add(new SimpleVector(0,-disBackLeftBottom,0));
vBackRight.add(new SimpleVector(0,-disBackRightBottom,0));
vBody.add(new SimpleVector(0,-disCenterBottom,0));
float tireFrontLeft = ground.calcMinDistance(vFrontLeft,v,dis*10);
float tireFrontRight = ground.calcMinDistance(vFrontRight,v,dis*10);
float tireBackLeft = ground.calcMinDistance(vBackLeft,v,dis*10);
float tireBackRight = ground.calcMinDistance(vBackRight,v,dis*10);
float bodyDis = ground.calcMinDistance(vBody,v,dis*10);
if (tireFrontLeft !=Object3D.COLLISION_NONE&&
tireFrontRight!=Object3D.COLLISION_NONE&&
tireBackLeft !=Object3D.COLLISION_NONE&&
tireBackRight !=Object3D.COLLISION_NONE) {
good = true;
//Vert Balance, rotation on X
double angleFront=((tireFrontRight+tireFrontLeft)/2-bodyDis);
double as=(angleFront/(bodyDisToFront))%1;
angleFront=Math.asin(as);
double angleRear=-((tireBackRight+tireBackLeft)/2-bodyDis);
as=(angleRear/(bodyDisToBack))%1;
angleRear=Math.asin(as);
float rot=(float) ((angleFront+angleRear)/2);
body.rotateX(rot);
//Hori Balance, rotation on Z
double angleLeft=((tireFrontLeft+tireBackLeft)/2-bodyDis);
as=(angleLeft/(bodyDisToLeft))%1;
angleLeft=Math.asin(as);
double angleRight=-((tireBackRight+tireFrontRight)/2-bodyDis);
as=(angleRight/(bodyDisToRight))%1;
angleRight=Math.asin(as);
rot=(float) ((angleLeft+angleRight)/2);
body.rotateZ(rot);
//move
body.translate(0,bodyDis,0);
return good;
}