A tough one, Game

I've got a networked driving game that drives cars around a 3D(DirectX) arena bounded by a wall. The problem is that while I can detect if one of the corners of the has hit a wall I can't figure out the maths of what how the car reacts afterwards.

All I can do successfully a straight bounce so that the reflected angle equals the incident angle. The problem lies in the left-hand co-ordinate system, which puts 90degrees to the left, but by calculation sin(90) is to the right as normal.

This is the code so far.
            //Calculate the incidint angle
                  D3DVALUE iAngle = (1.570796f - 6.28318f - m_pBoids[0].fYaw - d3dApp.wallAngles[wallID]);

            //calculate new angle
                  m_pBoids[0].fYaw += iAngle *2 ;
                  m_pBoids[0].fYaw = m_pBoids[0].fYaw - 6.28318f * (int)(m_pBoids[0].fYaw / 6.28318f);
                  if(m_pBoids[0].fYaw < 0)
                        m_pBoids[0].fYaw += 6.28318f;

There are 120 panels in the wall, and the angle of the panel hit is gotten using wallID.
The m_pBoids array is the array of cars.
A D3DVALUE is the same as a float.
The constants you see here are radian values e.g. 6.28318f = 360degrees

How do I calculate the angle car will end up in giving rough car collision dynamics and physics?

Please, no roundabout web addresses.
Who is Participating?
nietodConnect With a Mentor Commented:
its hard to understandand the meaning of what you have so far.  

But what you want to do is to breakt he car's velocity vector into 2 component vectors.  One that is parallel to the wall and one that is at right angles to the wall.  You leave the component vector that is parallel to the wall unaltered and you reverse the sign of the component vector that is perpendicar.  Then you combine these two component vectors to get the velocity vector after the collision.

I'll try to post an example shortly.
Actually, that seems to be kind of trivial, since you are not loosing mementum in this case (you are assuming a perfectly elastic colission)  so iin this case the velocity has the same magnitude and the direction is simply 90-the incident angle.

It might help if you were clearer about what figures you are working with and what figures you what to obtain,

but from what I can gather the only thing you are concerned about is the directionl (the speed doesn't change) so in this case the new velocity direction will be the wall's angle plus 180 minus the incident angle where the incident angle is the angle between the incomining velocity and the perpendicular to the wall.
Opps that should be plus 90
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

ajmcgarryAuthor Commented:
Thanks for the quick reply nietod,
I was hoping you would answer, with your background in physics.

I am actually worried about momentum but to keep things simple I left it out of the question.

If the incident angle is greater than 72degrees, roughly the angle through the opposite corner wheel, then the car should turn inwards, otherwise the car should turn outwards. Also because of the compression of the cars impact the resultant angle is not 90-incident.

I have to apply this to car to car collisions later, then I'll have fun.

Obviously the loss in momentum is equal to the (speed before)*cos(incident Angle)
ajmcgarryAuthor Commented:
The root of the problem is the reverse angle system, otherwise I could do it myself. It took me ages to come up with what I have. I allow it to always go minus and then make up the difference by adding 360 till its positive.
Can you explain to me the coordinate system you are using.  This "reverse angle" term is not familiar.   I guess I need to know

How is the wall angle specified (relative to what)
How is the car's velicity specified (angle (relative to what) and speed, or components).

Do you wish to absorb some energy in an impact, i.e have a loss of momentum?  There is a way to do that ithat will look good, without being to difficult, though I doubt it is scientifically accurate.  (I'm not sure...)  For this you will have to break the velocity into component vectors, then when you flip the sign of the perpendicular vector, you will also make it a little smaller, Maybe by about 50% or so, you'll have to play with the figure to see what you like.  What this does is allow the car to carry the same speed parallel to the wall, but loose speed perpendicular to the wall.  I guess that is what would happen if you hit a frictionless wall.  That tends to be the sort of algorithm used in games.

I'll be off line for a few (2-3) hours.  If you can give me a better idea of what you have to work with, and what you want (if you want to loose momentum) I can get you the equations.
ajmcgarryAuthor Commented:

270      ||       90
      car at 0


Car is at 0 when faces the wall at 0 (straight up)

But the car is at 270 when it faces the wall at 90. i.e. the cars angle system is the reverse of the walls.

I didn't program it this way it was a friend, the two of us are doing the game.
It was the only way he could get the rotational matrices to work.

The cars velocity and angle is relative to itself.
The angle of the walls are relative to the world.

The cars velocity is calculated using a
float for speed               (fSpeed)
float for angular direction   (fYaw)
Combined these give a single directonal vector.

As I said before I do want a loss of momentum, and like you said, what look good rather than what is scientifically sound.
ajmcgarryAuthor Commented:
**** that came out crap. If you don't understand my crap diagram, copy it to notepad or to the comment box below, it should read, slightly better.
ajmcgarryAuthor Commented:
I see what you're saying about the perpendicular vectors, but I was trying to do it by calculating the force of the impact (fSpeed * sin(incident)).

Would it not be right in saying that the force of the collision has an effect on how the car reacts.

After all, if the car were to approach slowly then it would simply scrap along the wall and not deflect.
I am not getting that.  

For walls, Are you saying that 0 is straight up and left is 270, like the diagram shows?

but what about cars?   It sounds lie the car's angle is relative to the wall.  But that makes no sense.  which wall?

>> The cars velocity and angle is relative to itself.
It can't be.  the velocify will be absolute (or realtive to the walls) that is fine, but what about the angle?  It ha to be relative to something.  otherwise the  angle is always the same for any direction the car is traveling.  That makes no sense.

>> float for angular direction   (fYaw)
That is probably the figure I need to understand.  What is his measured from?
ajmcgarryAuthor Commented:
Sounds like I have you tearing your hair out (sorry).

Just forget I mentioned the reversed angle at all, its my own fault and I'll fix that, or at least try. I can always work around it anyways.
ajmcgarryAuthor Commented:
Yes I fixed that ****y problem! Does that help. Obviously my friend is thick in the head.
huh?   are you going to post an answer to my question?  

Do I understand the wall part?
how is the car angle measured?  (From what and what direction is positive?)
Alright, here's what I have.  You might need to manipulate it for your situation or explain yours better.

I have both the wall's and the car's angle m,easured from the positive X axis.  That means that a wall that faces up is at an angle of 0 (it runs along the X axis)   A wall that faces left is at an angle of 90 (it runs along the Y axi a wall that fiaces down is at an angle of 180 (along the X axis, but facing down, etc...)  (obviously any angles between can be used, that was just for clarification.)

A car that is running to the right has an angle of 0 (along the x axis)  A car that is going straing up has an angle of 90 (along the y)  a car that is going down has an angle of 270. etc.

Now those are the simple angles the calculations may yield ugly angles but that should nto matter.  i.e the calculations might yield angles like 360, which is the same a 0 or -270 which is the same as 90.  However, those angles will work just as well, you just might not like them as much.

Given a vehicle  traveling at angle Av and a wall at angle Aw the collision angle will be

Av = Ac - Aw - 180

This is the angle of incident between the Wall and the vehicle.  (Again this might not yield the simplist angle, but it is correct--I hope.)  Using this angle you calculate the components of the velocity paralell to the wall and perpendicular to the wall.

Opps, I think I have a sign problem....
Wow it has been to long--10+ years.  I made things a little too complex.  
My math worked, but was overly complex,  For the easieast solution the collision angle will be just  

Ac = Av - Aw

No -180 like above.   (Also I seem to have switched Av and Ac above too).

then the veclocity component of the vehicle parallel to the wall (Vx) is simply

Vx = V cos Ac

where V is the car's velocity.  Note that this is not necessarily in the X direction!  The velocity perpendicular to the wall is

Vy = V sin Ac

I have a meeting--unexpected.  I'll get back to you.
I'm back--briefly

In a colission, the Vx will be unaltered.  (it could be reduced sligthly if there is friction with the wall, but games don't usually do that.)  Vx will be reversed in sign because of thebounce and it will be reduced in magnitude because of the loss of energy.  I would say your best bet is to just multiply it by a negative number that is between -1 and 0.  Like try multiplying by -.8.   this is probably not completely accurate, but shoudl give a reasonable feel.  (If you don't kuje ut we can look fo a better equation, it will probalby have to look at the kinetic entergy and mementym, so you would have to have some idea of the mass (well, maybe not....)

so anyways you get a new Vy like Vy = -.8Vy.  

Then you need to calculate the new V, this will be the magnitiude of the vector given by the two new componenet vactors.   You use the Phythagorean theorem for this.

Have to go.
ajmcgarryAuthor Commented:
Nietod thanks for all the help.

I know that I may have not explained things perfectly, sorry for that I seem to have it in my head but cant get it to paper.
Although your answer does not instantly solve my problem it gives me a good basis for solution given a little work.

I think an A is deserved for all the effort you have put in.
So the final velocity is the square root of the sum of the squares of the two components.  i.e

V = Sqrt(Vx*Vx + Vy*Vy)

to get the angle for the final velocify you can use the arc trig functions, like

A = arccos(Vy/V)

because of the cos is periodic the actual angle may be 180 more than that.  If Vy is negative, the actual angle should be 180 degrees more.

Now this angle is the angle relative to the wall, not the to the positive X axis, you need to add the wall angle onto to this angle to get the vehicle's angle, like

Av = arccos(Vy/V) + Aw

(plus 180 if the Vy is negative.)

Let me know if you have any questions.
You were a bit premature, I was compltely done.  That should be what you need, assuming the input information is correct.  i.e. the angles.
ajmcgarryAuthor Commented:
I managed to fix the angle problem.
Now a car that is travelling at 0 degrees forward along the y axis will hit a wall at 0 degrees which is perpendicular to the x axis and so on.

So basically all I do is change yours to
Ac = Av - Aw - 90
which is basically what I had in the first place.

Makes you laugh huh.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.