Login | Register

Car collision

October 6, 2006, 19:55 by Calle
[loading]
-->
Today I will retell a complete topic from the GMC which I answered to, and not as usual only a solution... because there are more than one solution to this and I really like the opinion of EyeGuy!

Speedmaster:
I'm making a GTA Clone, you may of seen it. Basicly, the biggest problems are the car collisions, i have tried negativing the speed and adding friction but it means ugly bounces, getting stuck and the disablity to scrape along a wall.

Does anyone have a decent engine for rotated sprite collision with those things working. I will give Huge credit and a billboard and a mention on my games radio stations for any engines submitted, used or not.

Cheers.

EyeGuy:
I always felt that car games would greatly benefit from the idea of seperated collision sensors. A big rotating block is a very vague thing to check collisions for. Obviously, if you run straight into the wall, it's different than if you brush up against it, but using normal collision functions, there's pretty much no way to find out where the car got hit. You end up trying to guess based on the speed and change in position and that just confuses the whole thing.

But if you seperate into specific parts of the car (say, front impact, side impact, back impact), the script ends up being much cleaner and much more stand-alone.

Take this diagram (forgive the horrible car):
user posted image

If you're going forward and you the Blue dot has a collision, then you ran straight into the wall and should bounce and cause major damage. But if a green dot has a collision, you're probably just scraping against the wall and not only do you know where the damage is, it's very simple to get out of the wall without stopping the car completely.

With 8 collision dots, you could make a very elaborate physics and damage engine without much code. It takes a little trigonometry to implement, but nothing more than "lengthdir_x(x_offset,angle)" etc.

Speedmaster:
Bummer, i would have to rewrite the entire Game code for every single car for that. I was kind of looking for something simpler, but a bit more advanced than:

speed = -3
friction = 0.2

I will have a go at using that in the near future now but because my game is at a point where a major change like that would just not be possible, i need something that can be done with just one event and object.

Credit going to Eyeguy!

And the finally me, not trying to make a breakthrough in Game Maker car engines or anything, just something simple:

xn = lengthdir_x(speed+1,direction);
yn = lengthdir_y(speed+1,direction);

if !(place_empty(xn,yn)) {
speed = 0;
}


The car will have no speed when it hits a wall. If your engine won't allow the car to turn around while speed equals zero, then you must change this. It could look good, I think. No bouncing. Try it out!

Reflection upon my own solution:
I especially want you to notice that I use speed+1; why? Speed is a variable which tells how far the object go each step. If the speed is high and my number is lower than the speed, then I won't stop the car until it has got stuck in the wall! But if I check in front of me, the distance the car is going to go and one more; I always know it can't get stuck. Smart, eh?

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games