# car angle calculation

3 replies to this topic

### #1 fel666

fel666

GMC Member

• GMC Member
• 76 posts
• Version:GM:Studio

Posted 12 February 2012 - 02:47 PM

I recently started a 3d car game (my second) but i over came a problem with changing the cars angle . this is the code i am curently using:
```
//x and y
lx=x+lengthdir_x(whidth/2,image_angle+90);
ly=y+lengthdir_y(whidth/2,image_angle+90);
rx=x+lengthdir_x(whidth/2,image_angle-90);
ry=y+lengthdir_y(whidth/2,image_angle-90);
fx=x+lengthdir_x(length/2,image_angle);
fy=y+lengthdir_y(length/2,image_angle);
bx=x+lengthdir_x(length/2,image_angle+180);
by=y+lengthdir_y(length/2,image_angle+180);

//z
lz=scr_terrain_get_z(lx,ly);
rz=scr_terrain_get_z(rx,ry);
fz=scr_terrain_get_z(fx,fy);
bz=scr_terrain_get_z(bx,by);
wzd=(lz-rz);
lzd=(fz-bz);

//ang
xang=(point_direction(ry,rz,ly,lz));
yang=(point_direction(bx,bz,fx,fz))+180;
```

but the car does not change angle correctly.
what code should i use to make it work correctly?
if you know any or know an other topic covering it, could u please post?

Edited by fel666, 12 February 2012 - 02:48 PM.

• 0

### #2 TheSnidr

TheSnidr

That guy

• Global Moderators
• 2472 posts
• Version:GM:Studio

Posted 12 February 2012 - 03:36 PM

You need to take the cross of two vectors between three points on the terrain below the car. This will give you the terrain normal vector at that point. Next you should make the car's to-vector perpendicular to the up vector by orthogonalizing it with this script:
```/*
orthogonalize(vx,vy,vz,x,y,z)
Makes a unit vector from [x,y,z] that is perpendicular to [vx,vy,vz]
*/
var l;
l=argument0*argument3+argument1*argument4+argument2*argument5
xx=argument3-argument0*l
yy=argument4-argument1*l
zz=argument5-argument2*l
l=point_distance_3d(xx,yy,zz,0,0,0)
xx/=l
yy/=l
zz/=l```
And then you transform the model with this script:
```/*
Rotates the model towards two perpendicular unit vectors.
*/
xx=argument1*argument5-argument2*argument4
attitude=arcsin(xx)
if abs(xx)>0.9999
{
bank=0
}
else
{
bank = arctan2(argument1*argument3-argument0*argument4,argument2*argument3-argument0*argument5)
}

Whether or not this post was useful depends entirely on how much you know about vectors. I don't have time to explain in further detail at the moment, sorry about that

Edited by TheSnidr, 12 February 2012 - 07:37 PM.

• 0

### #3 fel666

fel666

GMC Member

• GMC Member
• 76 posts
• Version:GM:Studio

Posted 12 February 2012 - 03:52 PM

Thanks, ill try that
• 0

### #4 Gamer3D

Gamer3D

Human* me = this;

• GMC Member
• 1619 posts
• Version:GM:Studio

Posted 12 February 2012 - 07:04 PM

You need to take the cross of two vectors between three points on the terrain below the car. This will give you the terrain normal vector at that point. Next you should make the car's to-vector perpendicular to the up vector by orthogonalizing it with this script:

Whether or not this post was useful depends entirely on how much you know about vectors. I don't have time to explain in further detail at the moment, sorry about that

He can just google it if he doesn't know.

Anyway, when choosing the points on the terrain, I recommend selecting 1 point under each back wheel (Player views that) and 1 point halfway between front wheels. That should give best visual results for this method.

One last thing: Kudos to you for using terrain instead of going for the "3D game in completely flat world" thing.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users