A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Top down car game collision question

Threaded View

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center