waypoint path movement, racing

BlitzMax Forums/BlitzMax Beginners Area/waypoint path movement, racing

redmoth(Posted 2011) [#1]
Hi, I'm trying to make a very simple simulation of a F1 race from above and have two problems. Here's a picture of what I've got at the moment:

link

The cars go from one checkpoint/waypoint to the next, which is basically a 2d array, which holds the speed of the checkpoint and then the x and y coordinates.

I've got the cars going around the track from one checkpoint to another, but the movement seems very jerky, the dots seem to be moving up and down really quickly every second. What I want them to do is go in a complete straight line between each checkpoint, so is this code correct?


Method moveSelf() 'move the car
		
nxtX = checkPoints[checkPoint+1,1]	'set nxtX to next checkpoint X
nxtY = checkPoints[checkPoint+1,2]	'set nxtX to next checkpoint Y

x:+(Sin(ATan2(nxtX-x,nxtX-x))*speed)	'move x 
y:+(Cos(ATan2(nxtY-y,nxtY-y))*speed)	'move y

end method




2. The thing is that I want to check which car is nearest the next checkpoint, from other peoples examples, Ive got this:

Local selfd:Int = Sqr( (Self.nxtX-Self.x)*(Self.nxtX-Self.x) + (Self.nxtY-Self.y)*(Self.nxtY-Self.y) )
								'			Local objectd:Ininfrontt = Sqr( (obj.nxtX-obj.x)*(obj.nxtX-obj.x) + (obj.nxtY-obj.yis)*(obj.nxtY-obj.y) )
							'		If selfd < objectd   'if the distance is less than the car infront
   'update positions here (which works fine)
end if


the only thing wrong is that the cars seem to be overtaking like crazy, I think it's due to the fact that they don't go in a straight line to each check point, as in problem 1

Last edited 2011


Warpy(Posted 2011) [#2]
Your ATan2 bit on both lines should be exactly this:
ATan2(nxtY-y, nxtX-x)



Floyd(Posted 2011) [#3]
How did Sin, Cos, ATan2 get involved in straight line movement?

You have two differences, call them dx and dy, which arise from nextValue-currentValue.
Think of these as a vector, which has length mag=Sqr(dx*dx + dy*dy).

Then Cos( ATan2( dy, dx ) ) is just a complicated way of saying x/mag.
Likewise Sin( ATan2( dy, dx ) ) is the same as y/mag.

The real issue is most likely that you are changing dx and dy continuously. They should stay the same as you move from one waypoint to the next.