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

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.

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.

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.

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)

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.

/\

270 || 90

--

car at 0

180

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.

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.

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?

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.

Do I understand the wall part?

how is the car angle measured? (From what and what direction is positive?)

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.

continues

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....

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

continues

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.

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.

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.

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.

Cheers.

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.

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.