gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > sin cos 3d rotation

#57487 - chrissieboy - Sun Oct 16, 2005 12:56 pm

Hi guys, im busy for 1,5 day to make my vehicle drive in the good direction.

So the up button is accelerate, left and right rotate the vehicle. And if they push left or right the vehicle is rotated and with up the vehicle must accelerate the direction is chosen.

How can i accelerate the vehicle in the good x and z direction?

now my vehicle only moves in the x direction. But i need a math based solution with sin and cos or something.

e.g :

z |............45 degrees
6 |.........../
5 |........./
4 |......./
3 |...../
2 |.../
1 |./
----------------
12345 6 x

vehicle is turned into 45 degrees, now the x and z moves egual.

If vehicle is turned 20 degrees then z moves faster then x .

lets say z = 6 and x is 3

Maybe somebody already had this problem too and know a solution?

#57500 - Zed0 - Sun Oct 16, 2005 4:03 pm

I've never actually made a game like this but I would have thought that what you basically wanted was something this sudo-code

Code:

R = Rotated degrees (up = 0 deg)
S = Speed
Zpos = Z Position
Xpos = X Position

if UP = 1
    accelerate(S,R)
else if DOWN = 1
    brake(S,R)
else if LEFT = 1
    lrot(S,R)
else if RIGHT = 1
    rrot(S,R)
else
    cont(S,R)
end if

------------

accelerate(S,R)
    S++
    if S > 10
        S = 10
    endif
    cont(S,R)
return

brake(S,R)
    S--
    if S < 0
        S = 0
    endif
    cont(S,R)
return

lrot(R)
    R--
    if R < 0
        R = 360 + R
    endif
    cont(S,R)
return

rrot(S,R)
    R++
    if R > 360
        R = R - 360
    endif
    cont(S,R)
return

cont(S,R)
    Zpos = Zpos + S * Cos(R)
    Xpos = Xpos + S * Sin(R)
return

#57767 - Zed0 - Tue Oct 18, 2005 9:30 am

Sorry for the double post but I was randoml thinking about this and thought of a better way to do it which would allow you to accelerate/break while turning corners

Code:

R = Rotated degrees (up = 0 deg)
S = Speed
Zpos = Z Position
Xpos = X Position

if UP = 1
    accelerate(S)
end if
if DOWN = 1
    brake(S)
end if
if LEFT = 1
    lrot(R)
end if
if RIGHT = 1
    rrot(R)
endif
cont(S,R)

------------

accelerate(S)
    S++
    if S > 100
        S = 100
    endif
return

brake(S)
    S--
    if S < 0
        S = 0
    endif
return

lrot(R)
    R--
    if R < 0
        R = 360 + R
    endif
return

rrot(R)
    R++
    if R > 360
        R = R - 360
    endif
return

cont(S,R)
    Zpos = Zpos + S * Cos(R)
    Xpos = Xpos + S * Sin(R)
    delay(100)
return