Adding collision to my car game?
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
April 06, 2003 06:25 AM
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...
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...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement