Login | Register

Bouncing script

December 21, 2006, 15:01 by Calle
[loading]
-->
First of all, there is a function called move_bounce_all() already, and I know this. However, if you know how it works then that'll extend the possibilities. That is why I will write a litte about it. I will create a function which can be used in your games if you want to have a more realistic bouncing. Because with the built-in function objects that bounce remains to have the same speed, and they bounce the same for all different objects. That is not how it is in real life.

In real life different objects react different with different objects. If you bounce a ball on a floor it will bounce higher than if you bounce it on a rug. It has to do with the friction between the two objects. We can describe the friction as a number between 0 and 1. When the percentage equals 1 there is no friction, but when it is 0, the friction is too large to make anything bounce at all. The speed is relative to the friction, and therefore the following formula will be true:

speed = e * speed;

Where e will be the bouncing factor. Speed will be defined a new value based on its old value decreased by the bouncing factor. But this is not all for this function, we will also need to determine the right direction. For this we assume that the reflection angle is equal to the angle from which it came. This is what they assume in the move_bounce_all() function. Therefore:

direction = 90-(direction-90);

And compiling this into a function:
//Name, arguments
bounce(speed,direction,bouncing_factor,object)
//The script
argument3.speed = argument0*argument2;
argument3.direction = argument1-2*(argument1-270);


And this'll be the script, and you can call it in the collision event of any object.

As you will probably want to call this in the ground-object, the object that doesn't move, it could look like this (in collision event of the floor for an example):

bounce(other.speed,other.direction,0.753,other);

Good luck!
Please tell me if there is something wrong (like the direction formula, I haven't tested it).

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