Advertisement

Adding collision to my car game?

Started by April 06, 2003 05:29 AM
1 comment, last by Xnin 21 years, 10 months ago
HiHow do i go about adding collision detection between the car and the walls in my simple game. Heres a picture example of what i mean. [removed] Do i constantly check the distance between the car and the every part of the wall or something? I know from a previous thread that i should treat the car as a cicle and calculate the sum of the radius between the 2 objects etc... Basically i want the car to stop every time it hits the walls (surrounding the car).Thank You :D:D
If you are making a lame game with walls bordering the car on the x and y axis, you could just use...

WallFriction=0.5;

if(LeftWall.x < Car.x - Car.Radius)
{
if(Car.velocity.x<0)
Car.velocity.x=-Car.velocity.x*WallFriction;
}
if(RightWall.x > Car.x + Car.Radius)
{
if(Car.velocity.x>0)
Car.velocity.x=-Car.velocity.x*WallFriction;
}
if(TopWall.y < Car.y - Car.Radius)
{
if(Car.velocity.y<0)
Car.velocity.y=-Car.velocity.y*WallFriction;
}
if(BottomWall.y > Car.y + Car.Radius)
{
if(Car.velocity.y>0)
Car.velocity.y=-Car.velocity.y*WallFriction
}

if you want walls anywhere you will need to do it properly

Also if you want to do it properly, find the closest distance between the wall and the car and then compare that against the radius of the car, if its smaller then reflect the cars velocity on the walls normal and then multiply the velocity by the walls friction...
Advertisement
Here are some links on car physics

http://www.gamasutra.com/resource_guide/20030121/kennedy_01.shtml <----- Need a gamasutra memebership, its free. The tutorial is very usefull

This topic is closed to new replies.

Advertisement