7
« on: April 10, 2007, 02:15:18 am »
I found another problem with my code. After finding the angles, I move the tank directly down instead of in the direction the base of the tank was facing, so I fixed that. Now the tank moves better over the map, but it wobbles at times when it is not supposed to and on a flat surface in certain positions the tank rotates again out of control and gets stuck with its face in the ground.
Here's the improved code
maxVertDis is the max ascention the tank may take
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);
v = v.normalize();
float dis = 300f;
boolean good = false;
Object3D body = getBody();
//move up based on tank pos for ascension
body.translate(v.x*-maxVertDis,v.y*-maxVertDis,v.z*-maxVertDis);
float disFrontLeftBottom = getTireLeft().rayIntersectsAABB(
getFrontTireLeft().getTransformedCenter(),v,true);
float disFrontRightBottom = getTireRight().rayIntersectsAABB(
getFrontTireRight().getTransformedCenter(),v,true);
float disBackLeftBottom = getTireLeft().rayIntersectsAABB(
getBackTireLeft().getTransformedCenter(),v,true);
float disBackRightBottom = getTireRight().rayIntersectsAABB(
getBackTireRight().getTransformedCenter(),v,true);
float disCenterBottom = getTireLeft().rayIntersectsAABB(
getTireInsideLeft().getTransformedCenter(),v,true);
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(v.x*-disFrontLeftBottom,
v.y*-disFrontLeftBottom,v.z*-disFrontLeftBottom));
vFrontRight.add(new SimpleVector(v.x*-disFrontRightBottom,
v.y*-disFrontRightBottom,v.z*-disFrontRightBottom));
vBackLeft.add(new SimpleVector(v.x*-disBackLeftBottom,
v.y*-disBackLeftBottom,v.z*-disBackLeftBottom));
vBackRight.add(new SimpleVector(v.x*-disBackRightBottom,
v.y*-disBackRightBottom,v.z*-disBackRightBottom));
vBody.add(new SimpleVector(v.x*-disCenterBottom,
v.y*-disCenterBottom,v.z*-disCenterBottom));
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&&
bodyDis !=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 based on new
body.translate(v.x*bodyDis,v.y*bodyDis,v.z*bodyDis);
saveToLastLoc();
}else
{
resetToLastLoc();
}
return good;
}