Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075919 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:21:48 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Bouncing ball physics
Pages: [1]
Print
Author Topic: Bouncing ball physics  (Read 618 times)
Qqwy
Level 1
*


To who might ever read this: I love you!


View Profile WWW
« on: September 09, 2013, 06:34:47 AM »

I am developing a 2d shooter game, in which some of the bullets should 'bounce' off walls, rectangles and other objects in an realistic fashion.
In simple games I made before, it was enough to check the side of the rectangle and then negate either the X velocity or the Y velocity.
However, there are two problems with using this approach in my current game:
-I am using rotation and directional speed instead of a separated X and Y speed for my bullets.(In other words, bullets move forward with a certain speed in the direction they are currently facing)
-Some of the walls and rectangles might be rotated, so not fully perpendicular with the X or Y axes.


Now I know from my Physics classes in school that when bouncing objects like a light particle on a surface, the incoming and the outcoming direction should have the same angle with the normal of the surface. However: How can I change the angles of my game objects to an angle relative to the normal?
Logged



Ř̺͈̮ͬͣ͑͂͊̐a̲͈̲̩̫͍̟̕i̪̪̩̼̩̊̽ͫn̴b̗̠͈̯̲͡ͅo̥̤͓̥̩̾͐ẅ̺́͢ ̴̙̑̍̅o̰̹͙̻̭̘̅͌͐̾ͅf̖̖͖͍̽̅̉͡ ͓̱͓͔̖̣̗ͭC̽҉̗̼̳̖͇̳h̺͕͠a̵̾ͤ͆́́o̼̙͖͎͍̳̅̿ͣs͓̒̌̀  FOCUS-Bytebeat
motorherp
Level 2
**



View Profile WWW Email
« Reply #1 on: September 09, 2013, 06:47:39 AM »

If your ball's incoming speed is a vector 'vin' and the collision surface normal is a vector 'n', then your balls outgoing speed vector 'vout' will be:

vout = vin - (n * (1 + e) * n.vin)

where '.' is the vector dot product, and 'e' is the restitution of your wall (0 = no bounce, all energy absorbed,   1 = complete bounce, all energy is reflected).
Logged

Qqwy
Level 1
*


To who might ever read this: I love you!


View Profile WWW
« Reply #2 on: September 09, 2013, 12:02:10 PM »

Thank you very much for your reply. I do have a few more questions concerning these vectors, though:   Facepalm
-As I understand it, a vector is a 'force'(in this case speed) in a certain direction?
-what size of force should the normal have?
-When calculating vectors like this, should I calculate the speed and rotation values of the vector separately or should I somehow combine them first?
To clarify:
Is it:
Vout.x = Vin.x - (n.x * (1+e) * n.x DOT Vin.x;
Vout.y = Vin.y - (n.y * (1+e) * n.y DOT Vin.y;

or something along the lines of:
Vout = (Vin.x+Vin.y) - (n.x+n.y)*1+e)*(n.x+n.y) DOT (Vin.x+Vin.y) ;
?
- The dot product is (n.x*n.y)+(Vin.x*Vin.y), right? Javascript does not have one built in. However, this makes me even more confused concerning the previous question.

Logged



Ř̺͈̮ͬͣ͑͂͊̐a̲͈̲̩̫͍̟̕i̪̪̩̼̩̊̽ͫn̴b̗̠͈̯̲͡ͅo̥̤͓̥̩̾͐ẅ̺́͢ ̴̙̑̍̅o̰̹͙̻̭̘̅͌͐̾ͅf̖̖͖͍̽̅̉͡ ͓̱͓͔̖̣̗ͭC̽҉̗̼̳̖͇̳h̺͕͠a̵̾ͤ͆́́o̼̙͖͎͍̳̅̿ͣs͓̒̌̀  FOCUS-Bytebeat
Xienen
Level 3
***


Greater Good Games


View Profile WWW
« Reply #3 on: September 09, 2013, 05:09:02 PM »

So, rather than "speed" vector, I believe you really want the velocity vector, which includes direction/rotation and speed.
The normal should be unit length or have a size of 1.
You can calculate the X and Y result separately, but as mentioned, you will be inputting and calculating a velocity vector(speed and rotation combined).

I would strongly suggest downloading a 2D Vector class that includes calculations for things like dot product.  It will help a lot with the debugging.
Logged

ClockworkWolf
Level 0
**


Tick Tock, Clockwork Wolf


View Profile WWW Email
« Reply #4 on: September 09, 2013, 06:13:59 PM »

I was experimenting with the code mentioned above, but I couldn't seem to get it to work, but after reading this: http://mathworld.wolfram.com/Reflection.html

It seems like I normalized my incident vector when I shouldn't. Got stuff to work now Smiley

Anyways, in rely to Qqwy:
A Vector simply indicates a direction. In addition, if you take the magnitude of the vector, it can be used to represent a length, or speed.
The normal simply indicates a direction of a polygon's face. In your case I think it is the normal of the polygon being bounced off. It should be normalized to a unit vector (i.e. length of 1)
The dot product takes in two vectors you shouldn't need to split them up as you indicated.

Not sure what you are coding in, but it should be something like:
Vnew = Vprev - 2(Vprev.Nn)*Nn
Vout.x = Vnew.x
Vout.y = Vnew.y

Where Nn = normalized normal (length 1)

An alternative is this other answer I found, which uses two cross products
http://www.gamedev.net/topic/204055-calculating-reflection-vector-i-have-no-math-book-here/

But I'm not sure what kind of performance using cross products will give.
Logged
Qqwy
Level 1
*


To who might ever read this: I love you!


View Profile WWW
« Reply #5 on: September 10, 2013, 10:51:42 AM »

Thank you very much.
I am using Javascript. Javascript does not have built-in Vector objects, and therefore it all becomes much harder. There are a few libraries(such as this one) that do some of the vector math for you, but you have to call them using functions instead of the standard math operators. They also are badly documented.

Because vectors are stored as a number pair, and these pairs also get labeled "x" and "y" it is still not clear to me if a vector is a cartesian coordinate, or a (direction) (velocity) pair. And in the second case, in which order should the values be stored? Or does this not matter? Does it matter if you use radians or degrees? Or are both these things irrelevant, as long as you store all your vectors this way?


I tried the following code to bounce objects up when they hit the bottom of the screen.

Code:
var Vin = new Vec2(this.rotation, this.speed);
var Vnormal = new Vec2(Math.PI, 1);
var Vnormal2 = new Vec2(Math.PI,1);

var e = 1;
vMath.dot(Vnormal, Vin);//Vnormal now is: (Vnormal . Vin)
vMath.mulS(Vnormal,1+e );//Vnormal now is: (1+e)*(Vnormal . Vin)
vMath.mulV(Vnormal,Vnormal2 );//Vnormal now is: Vnormal * (1+e)*(Vnormal . Vin)
vMath.subV(Vin, Vnormal);//Vin now is: Vin - (Vnormal * (1+e)*(Vnormal . Vin)
var Vout = Vin;//Full statement: Vout = Vin - (Vnormal * (1+e)*(Vnormal . Vin)
this.setRotation(Vout.x);

This does not work. Using some console.log statements to check the values inbetween the steps,  I get back  the following feedback in the console:

Code:
this.rotation = 0
this.speed = 0.01
(Vnormal . Vin) x: 3.141592653589793 y:1
(1+e)*(Vnormal . Vin) x:6.283185307179586 y:2
Vnormal * (1+e)*(Vnormal . Vin) x:19.739208802178716 y:2
Vin - (Vnormal * (1+e)*(Vnormal . Vin) x:-19.739208802178716 y:-1.9
final result: Vec2 {x: -19.739208802178716, y: -1.9}

This does not at all seem right to me. The rotation value should become either 0 or 2PI, and the speed should stay at 0.01, since I gave in full restitution.

Hopefully someone knows what I am doing wrong.
Logged



Ř̺͈̮ͬͣ͑͂͊̐a̲͈̲̩̫͍̟̕i̪̪̩̼̩̊̽ͫn̴b̗̠͈̯̲͡ͅo̥̤͓̥̩̾͐ẅ̺́͢ ̴̙̑̍̅o̰̹͙̻̭̘̅͌͐̾ͅf̖̖͖͍̽̅̉͡ ͓̱͓͔̖̣̗ͭC̽҉̗̼̳̖͇̳h̺͕͠a̵̾ͤ͆́́o̼̙͖͎͍̳̅̿ͣs͓̒̌̀  FOCUS-Bytebeat
ClockworkWolf
Level 0
**


Tick Tock, Clockwork Wolf


View Profile WWW Email
« Reply #6 on: September 10, 2013, 12:10:08 PM »

Qqwy, if we are on 2D vectors, the two values in a vector stores the displacement in the X and Y axis, not (direction,velocity).

Hence,
These vectors: (1,1) and (0.7,0.7), (3.142,3.142) all point in the same direction. What differs, is their length (aka magnitude, length etc)

You might find doing a vector maths tutorial like this one:
http://tutorial.math.lamar.edu/Classes/CalcII/Vectors_Basics.aspx

will provide you with lots more illumination on what a vector is. I can use vectors to represent 3d co-ordinates, direction&speed, color, uvs, etc.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic