Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / solving 2D rebound angles using trig

Author
Message
Suicidal Sledder
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location: Tikrit, Iraq
Posted: 13th Sep 2010 02:36 Edited at: 15th Sep 2010 05:25
What trig functions can be used to determine the rebound angle of an object striking a fixed object? I am using 2 variables xmove# and ymove# that equal how far the object moves every loop.

I am fairly certain I need to do make a right triangle out of the two objects and use trig functions. for example if xmove# and ymove# = -1 then my object is moving up and right. now if it strikes another fixed object perpendicularly(ie if it is at a 45 degree angle from the other object) then I have created a right triangle. like this:


where p is the player (movable object) and S is the solid object. If it did this at 45 degrees it would reflect back at 225 degrees(45 + 180). now to translate this to my movement xmove# =-1 and y move = 1. Down and left.

I get the THEORY but not the technically know how to code this algorithm. I'm pretty sure I need to bisect the angle (the normal i think) but I cant quite nail this down.

Any suggestions? thanks.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 13th Sep 2010 05:09
Basically, say you have something that looks like this:


angle c is the angle that the ball is colliding in, or wrapvalue(atanfull(vx,vy)-180) or atanfull(-vx,-vy).

angle b is the angle of the normal of the wall

angle a is the angle after the ball has collided from the wall.

What we want, is to have the difference between b and c, the same as the difference between b and a. We can state this mathematically by saying
c-b=-(a-b)
solving for a, a=2*b-c

So... that's simple. Lets test it out.


You could also just use vectors.

BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 13th Sep 2010 05:17
Alright, this will dip into trig and some physics. I will include some shortcuts at the end. These are probably faster and easier, but you don't really know whats going on (great when it works, a living hell when it doesn't)

I apologize in advance if this gets really mathy, I will do my best to keep it simple.

Note that all labels in bold are vectors, if they aren't, then they are the magnitude of the vector.

First off, you want to stop the object from going through the obstacle. To do this, we will use vectors. V1 will be your motion vector for the player/object. V2 will be the vector that is normal to the surface being collided with (the obstacle). NOTE: Keep V2 as a UNIT vector, that means it is only 1 unit in length. If you want an explanation of how to find v2, I can give it (quickly: find the angle, find the normal, use trig to find the components).

First off, you perform the Dot product on the two vectors which project V1 onto V2 (which will show how big V2 needs to be to stop V1 from going through it). Here is what the math looks like:
abs(V1x*V2x+V1y*V2y)

This will give the magnitude of V3 (the vector that represents V1 projected onto V2).

Now for the physics.

The law of conservation of momentum states that in all collisions momentum is conserved.

Momentum is found by this formula:
P=M*V
Where P is Momentum, M is mass and V is the magnitude of the velocity.

So, because momentum is conserved, Pstart=Pfinal.

Pstart=M*V1
Pfinal=M*Vf

Vf is a new vector used to represent the final velocity vector of the object's motion. It can be expressed like so:

Vf=V1-V3+R

R is a variable to show that we will have to add something to make it work.

Since they are vectors, you can look at it like this:
Vf=<V1x-V3x+Rx,V2y-V3y+Ry>

So, to put it all together. We should have found V3 by now and we have:

M*V1=M*Vf
V1=Vf

Note that V1=Vf is NOT the same as V1=Vf (the components will be different but the magnitudes will be the same).
Now the math gets....irritating, so here is the shorthand version:

R=-V3

Meaning:
Vf=<V1x-2*V3x,V1y-2V3y>

Congrats you just solved science!!!


SHORTCUTS!!!!

These are solutions that SHOULD work, but I haven't tried. They will be shorter and easier to implement but probably harder to debug.

1. Reflection Matrix


Phi is the angle of the obstacle you hit.
I will spare you the math and give you the results.

These are just to make it easier on the eyes
Mtx1=2cos^2(Phi-1)
Mtx2=2cos(phi)sin(phi)
Mtx3=2cos(phi)sin(phi)
Mtx4=2sin^2(phi)-1

Vfx=Mtx1*V1x+Mtx2*V1y
Vfy=Mtx3*V1x+Mtx4*V1y

2. The angle between the obstacle and the V1 vector is the same as the angle between the obstacle and the Vf vector. Use some basic trig to figure things out. Can explain if necessary.


Can't think of anything else. Personally, I would just use the reflection matrix. However, those are much harder to debug than say doing the whole process at the beginning. Shortcut 2 is probably the simplest, but might give you some trouble here and there.

Hope I didn't lose you though all this!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Suicidal Sledder
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location: Tikrit, Iraq
Posted: 13th Sep 2010 05:22
wow dude that is freaking incredible... I am working rite now on translating it and plugging it into my program



Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 13th Sep 2010 08:49
for vectors, what you get after calculating out what BN2 wrote (i'm pretty sure xD)

Vfx=Vx-2(Vx*Nx+Vy*Ny)*Nx
Vfy=Vy-2(Vx*Nx+Vy*Ny)*Ny

where N is a vector length one that is the normal of the surface.
(going with angles a,b,c like above: N=<cos(b+90),sin(b+90)>

With vectors, this is simply:
Vf=V-2*(N.V)*N

The heart of the thing is that the dot product N.V=|N|*|V|*cos(theta)=Nx*Vx+Ny*Vy where theta is the angle between the two vectors, and |X| represents the length of vector X. Since the length of N is one, N.V=|V|*cos(theta), which can be interpreted as "The magnitude of V in the direction N). No trig necessary! If you have to convert from vector to angles then back to vectors, you probably should just use that.

Suicidal Sledder
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location: Tikrit, Iraq
Posted: 15th Sep 2010 05:25
awesome thank you everyone! I got it to work although it took a while and a lot of F5s lol. My space simulator is coming along very nicely and I can continue my work. thanks again everyone!

Login to post a reply

Server time is: 2026-07-22 22:06:56
Your offset time is: 2026-07-22 22:06:56