🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to implement raycast car?

Started by
17 comments, last by Bow_vernon 13 years, 4 months ago
Hello I'm writing a physics engine for my semester project. The basic rigidbody is done, they collide and bounce and rest. Now Id like to implement raycast car but am simply dont know how. Ive googled and it didnt help at all. Can you give me the basic pointers? I'll try to do the rest.
Thanks
Advertisement
Don't know how detailed you want to be with your physics, but:

1. model the suspension for each wheel ( spring, shock ) - the spring constant should support 1/4 of the weight of the chassis when the wheel is "mid-position" (i.e., compressed 50%)
2. ray-cast downward from each wheel to find local ground position. Determine if wheel needs to move up or down to rest on the ground. Check limits for position. I.e., the wheel can't rise above the suspension point at the chassis and can't drop below some fixed length for full spring extension.
3. the upward spring force on the chassis at each wheel position will be proportional to the new position with respect to the chassis. I.e., f = k*x, where x = 1 when wheel is up against the chassis; x = 0 when wheel is at full extension of spring.
4. the shock force is a function of the velocity*, where wheel_velocity = ( wheel_position - previous_wheel_position )/deltaTime. Note: this is the velocity along the axis of the suspension.
*Google for equations or start with velocity-squared and see how you like it.

Note: the shock force always opposes the movement. I.e., always in the opposite direction of the wheel velocity. Adjust spring and shock constants to suit.

Calculate the total force on the chassis (sum of the forces at each suspension point). F = ma, etc. Calc torque on the chassis center-of-mass if you want realism.

Move/orient the chassis. Advance the time. Repeat.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

wow thanks for the reply. before implementing, I've a question:
1. should I "push" the chassis along the direction of suspension or along the ground normal(found in collision)
2. about the wheel angular velocity, how to relate them with the friction?
3. I didnt see angular velocity mentioned
4. thx

wow thanks for the reply. before implementing, I've a question:
1. should I "push" the chassis along the direction of suspension or along the ground normal(found in collision)
2. about the wheel angular velocity, how to relate them with the friction?
3. I didnt see angular velocity mentioned
4. thx

1. The chassis is suspended by the suspension. The vector forces from the suspension act on the chassis center-of-mass. Because those force vectors are not normally through the center-of-mass, you'll need to resolve those into forces through the center-of-mass plus torques.
2. Are you talking about wheels driven by the engine, or angular velocity due to the wheels' friction with the ground?
3. If you're talking about angular velocity of the chassis, see 1 above. Other than that, I didn't see angular velocity mentioned either. ;) What angular velocity are you thinking of?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Ops sorry, I mean the wheel's spees of rotation(along the axle), and how would I handle cornering? what about wheel's friction with the ground?
anyway, I managed to handle suspension by applying force along normal at collision point. looks nice, but I need traction and sideway forces too. but how?
For a fairly simple implementation, you can separate the forces on a wheel along 3 axes and calculate those forces separately. Then the resulting forces (for each wheel) are applied to the chassis in 3D to determine the chassis's movement and rotation.

Consider 3 orthogonal axes at the point where the wheel joins the axle (modeling it with some radius from the edge of the wheel for angular velocity considerations later if you want).

Vertical - along the direction between the axle and the point where the suspension connects to the chassis. Movement along the vert axis is affected by the sping, the shock absorber and vertical forces from the surface (if the wheel is in contact with the surface). This is the axis about which the other 2 axes will rotate (for steering primarily).

Along the axle - the axis about which the wheel rotates. Forces along this axis will result from lateral friction forces of the wheel with the ground when there is lateral velocity of the wheel with respect to the surface - e.g., when the axle is turned for steering, or when the car is skidding sideways. That friction force is proportional to the force of the wheel on the ground (along the vertical axis) and always opposes (is in the opposite direction of) the lateral velocity of the wheel.

"Forward" - orthogonal to the other 2 axes above - the "foreward" direction of the wheel. Forces in this direction are due to friction of the wheel with the surface parallel with this axis but displaced by the radius of the wheel. That friction force is also proportional to the vertical force of the wheel on the ground, and always opposes the linear velocity of the wheel parallel with this axis.

Friction is a funny thing (actually not all that well understood). For the purposes of your simulation you can consider 3 types of friction:

Static - nothing is moving relative to one another. This friction opposes movement in any direction and has to be overcome to get the car moving. I'd suggest you ignore the simulation of static friction either forever, or until you get the rest of the simulation working. Otherwise, google is your friend.

Sliding - the wheel is translating in the direction in question but not rotating about the axis perpendicular to the direction. This applies to the axle axis, the lateral direction. The wheel does not rotate about the forward direction which is perpendicular. A lower coefficient of friction than rolling.

Rolling - the wheel is rotating about the axis perpendicular to the direction in question. This applies to the forward axis. The wheel is rotating about the axle, which is perpendicular to the forward axis. A higher coefficient of friction than sliding.

When the car is rolling forward, wheel friction with the ground is a force in the backward direction at the bottom of each wheel. That force resolves into a torque about the axle and a backward force on the suspension. The backward force acts as a torque on the chassis (the suspension doesn't move fore-back with respect to the chassis) and the torque on the wheel acts on the angular inertia (moment of inertia) of the wheel, causing the wheel to rotate.

If the wheel's axle is not perpendicular to the wheel's linear velocity (wheel is turned and/or the car is "skidding"), there will be a lateral force along the axle which will be a torque on the chassis.

"Traction" is a torque from the engine on each driving wheel (not steering unless it's front-wheel drive) that resolves to a torque applied to the moment of inertia of the wheel and a force at the point of contact (1 radius away) which opposes the rotation.

The reason I mentioned the types of friction to consider and mentioned torques is because you talked about traction and cornering. The forces in various directions determine the linear velocities of the wheel and chassis, and the torques determine the angular velocity of the wheels and chassis.

If you're interested in all of that, you'll probably want to maintain separate force, torque, position**, linear and angular velocity vectors for each wheel and for the chassis. At the beginning of each simulation cycle, zero out all the forces and torques, do all the calcs (accumulating forces and torques on each component as appropriate), calculate the velocities, calculate the position - repeat.

**Obviously the wheel position is a function of the chassis position as well as the surface.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I did what you suggest with a bit of hack. It's working now, but skidding seems pretty difficult, I need the car to skid a little more. here's how I calculate lateral and longitudinal force:
longmag=v*frontdir; //clamped to maxlong
latmag=v*steerdir;//clamped to maxlat
v is the velocity of wheel
Doublepost(phone's limited capacity)
frontdir is the wheel's direction(absolute)
steerdir is perpendicular with frontdir and upvector. I need to skid the car but well it's hard
and I dont know how to calc load on the wheel, right now the car pretty much follow the turn,which is boring...

I did what you suggest with a bit of hack. It's working now, but skidding seems pretty difficult, I need the car to skid a little more. here's how I calculate lateral and longitudinal force:
longmag=v*frontdir; //clamped to maxlong
latmag=v*steerdir;//clamped to maxlat
v is the velocity of wheel

Are you sure you mean force? Not sure what you're hacking, actually. But, if it works for you.

For effect, I cheat also and decrease the lateral friction coefficient just a bit based on the angular velocity (rotation speed) of the wheel. Seems to help when the chassis leans in a turn, which increases the lateral force due to friction on the outside tires.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Hey I thought about that too. Anyway, did you lower the friction of all tyres or just the rear ones? I will do what you suggest tonight. Anyway, v was the velocity of the contact point. I separate lateral and longitudinal friction coeffs. How about you?
thanks for the in depth explanations

This topic is closed to new replies.

Advertisement