I am making a top down view car game and could do with some advice on collision detection. I have read numerous posts about these types of games but havent seen anything specific to my question.
I can detect the collisions no problem. The problem I am having is determining how far the car can move to just before the collision.
To detect collisions I am using 5 points around the center of the car and using hitTest(x, y, true) function. I calculate the position of the points if the car moved at its current speed. If a collision is detected at this new position, I only want to move the car enough so it just reaches the collision point.
The track is quite irregular shaped so I am not sure how best to calculate how far the car can move so it just gets to the edge of the track.
Here is the basic code for detection of a collision with one of these points
Whils
Any advice much appreciated. If there is a better way of doing collision detection on this type of game, I would be most interested to hear about it. Btw I am creating this game in Flash 8 with AS2.Code:var frontX = car_mc.front._x; var frontY = car_mc.front._y; car_mc.frontNode = {x:frontX, y:frontY} car_mc.localToGlobal(car_mc.frontNode); var newFrontX = car_mc.frontNode.x + speedX; var newFrontY = car_mc.frontNode.y + speedY; if (terrain.hitTest(newFrontX, newFrontY, true)){ hitStateTxt.text = "HIT" } else { hitStateTxt.text = "SAFE" // No collision so move normally car_mc._x += speedX; car_mc._y += speedY; }
Paul




Reply With Quote