Direction Control

BlitzMax Forums/BlitzMax Beginners Area/Direction Control

Emmett(Posted 2006) [#1]
I know its in here somewhere but cannot find it.
Where is information on how to control the direction that a vehicle travels.
Ive been able to point the nose of the vehicle and move it in only 8 directions.
There must be a way to point it in any direction and make it go straight ahead.
Any help appreciated.
Emmett


tonyg(Posted 2006) [#2]
sin, cos and atan2.


Tom Darby(Posted 2006) [#3]
To expand on tonyg's comment, you'll want to get into some trigonometry: http://en.wikipedia.org/wiki/Trigonometry

In addition to x and y positions, you'll want to give your vehicle object velocity and heading information, too. Here are some of the variables I like to use:

x#
y#
dx#
dy#
heading#
velocity#
acceleration#

(Where heading is an angle between 0 and 360, and dx# and dy# fall on the unit circle.

Every game turn, move your vehicle thus:

x# = x# + (dx# * velocity#)
y# = y# + (dy# * velocity#)

To change your velocity, dx, and dy based on your current heading and acceleration, use vector math and trigonometric functions...


Emmett(Posted 2006) [#4]
Fantastic,
Kind of figured I would have to study trig. Was looking at it last night but was not sure. Am now.
Thanks for the help.
Emmett


Emmett(Posted 2006) [#5]
OK, the truck moves correctly.
I'm very stuck on making the trailer move realistically forward and backward.
Would really appreciate a helping hand here.
The truck.png is a square 21x21, the trailer.png is a rectangle 21x62.
Graphics 800,600
Global truck = LoadImage("media/images/truck.png")
MidHandleImage truck
Global trailer = LoadImage("media/images/trailer.png")
SetImageHandle trailer,10,1
Global truckx#=320, trucky#=240, truckangle#=0, trailerangle#=0, thrustdx#=0, thrustdy#=0
While Not KeyDown(key_escape)
	Cls
	SetRotation truckangle
	DrawImage truck,truckx,trucky
	SetRotation trailerangle
	DrawImage trailer,truckx,trucky+12
	If KeyDown(KEY_LEFT) And KeyDown(key_up) truckangle:-1 trailerangle:+1
	If KeyDown(KEY_RIGHT) And KeyDown(key_up) truckangle:+1 trailerangle:-1
	If KeyDown(KEY_LEFT) And KeyDown(key_down) truckangle:+1 trailerangle:-1
	If KeyDown(KEY_RIGHT) And KeyDown(key_down) truckangle:-1 trailerangle:+1
	If KeyDown(key_up)
		tdx#=Cos( truckangle-90 ) * 0.03
		tdy#=Sin( truckangle-90 ) * 0.03
		thrustdx = thrustdx + tdx
		thrustdy = thrustdy + tdy
	EndIf
	If KeyDown(key_down)
		tdx#=Cos( truckangle-90 ) * 0.03
		tdy#=Sin( truckangle-90 ) * 0.03
		thrustdx = thrustdx - tdx
		thrustdy = thrustdy - tdy
	EndIf
	truckx:+thrustdx
	trucky:+thrustdy
	thrustdx:*0.955
	thrustdy:*0.955
	If truckx>GraphicsWidth()+14 truckx=-14		'bounds check
	If truckx<-14 truckx=GraphicsWidth()+14
	If trucky>GraphicsHeight()+14 trucky=-14
	If trucky<-14 trucky=GraphicsHeight()+14
	FlushMem
	Flip
Wend