|
-
Top down car game collision question
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
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;
}
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.
Paul
Last edited by chuckylefrek; 09-18-2007 at 11:41 AM.
Paul Steven
http://www.mediakitchen.co.uk
-
I have managed to get it sort of working but I think it could be improved
http://www.mediakitchen.co.uk/testCollision.html
Basically if I detect there will be a collision on the next movement at the current speed, I run a loop for all speeds from 1 to this speed and check if there would be a collision for these smaller speeds. If there is then I break out of the loop and use that speed value
Code:
for (var i = 0; i < Math.floor(Math.abs(snail.speed)); i++) {
speedX = Math.sin(rotationValue*(Math.PI/180))*i;
speedY = Math.cos(rotationValue*(Math.PI/180))*i*-1;
newFrontX = snail_mc.frontNode.x + speedX;
newFrontY = snail_mc.frontNode.y + speedY;
if (terrain.hitTest(newFrontX, newFrontY, true)){
break;
}
Paul Steven
http://www.mediakitchen.co.uk
-
When I wrote http://nonoba.com/chris/flash-sliders I simply used getPixel on a hitmap stored in a bitmapData. The getPixel is taken at the center of the car, and if something is hit, the car is moved back to its previous position and the motion vector set to 80% in the oppressive direction.
Its really really simple, but works pretty well. Additionally it also allows for other ground types like the grass and the water. Each type of ground simply got a separate color on the bitmap hitmap.
Hope it helps
/Chris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|