Jump to content

Hacking WheeledVehicle for OldSchool steering (Car rotation)


Recommended Posts

Hi!

I'm currently working on a new version of my game

. I decided to rewrite the custom vehicle class I'm using for a while since It produce some weird error "bins" that I have never been able to figured and I think I can do better with my enhanced C++ knowledge.

My previous vehicle class was worked based on the Hover Vehicle class since I want that Old-School steering which make the car rotate arround it's center instead of realistic wheels steering. Now I'd like to based my new class on the WheeledVehicle since it's deal better with physic (Suspension, roll over, etc). I did some experiment to add the HoverVehicle steering code but I can't get it to work since (I think) some wheels forces are preventing the car to simply rotate. I'm optimistic to be able to find a work-around but I'm curious to see if someone have an idea about how it could be done simply. I want to keep the WheeledVehicle class code the more intact I can.


I suspect the tires forces part of code is what I need to modify to allow to freely rotate the car:

 // Tire direction vectors perpendicular to surface normal
        Point3F wheelXVec = bx * cosSteering;
        wheelXVec += by * sinSteering * wheel->steering;
        Point3F tireX, tireY;
        mCross(wheel->surface.normal, wheelXVec, &tireY);
        tireY.normalize();
        mCross(tireY, wheel->surface.normal, &tireX);
        tireX.normalize();

        // Velocity of tire at the surface contact
        Point3F wheelContact, wheelVelocity;
        mRigid.getOriginVector(wheel->surface.pos,&wheelContact);
        mRigid.getVelocity(wheelContact, &wheelVelocity);

        F32 xVelocity = mDot(tireX, wheelVelocity);
        F32 yVelocity = mDot(tireY, wheelVelocity);

        // Tires act as springs and generate lateral and longitudinal
        // forces to move the vehicle. These distortion/spring forces
        // are what convert wheel angular velocity into forces that
        // act on the rigid body.

        // Longitudinal tire deformation force
        F32 ddy = (wheel->avel * wheel->tire->radius - yVelocity) -
           wheel->tire->longitudinalRelaxation *
           mFabs(wheel->avel) * wheel->Dy;
        wheel->Dy += ddy * dt;
        Fy = (wheel->tire->longitudinalForce * wheel->Dy +
           wheel->tire->longitudinalDamping * ddy);

        // Lateral tire deformation force
        F32 ddx = xVelocity - wheel->tire->lateralRelaxation *
           mFabs(wheel->avel) * wheel->Dx;
        wheel->Dx += ddx * dt;
        F32 Fx = -(wheel->tire->lateralForce * wheel->Dx +
           wheel->tire->lateralDamping * ddx);

        // Vertical load on the tire
        verticalLoad = spring + damping + antiSway;
        if (verticalLoad < 0)
           verticalLoad = 0;

        // Adjust tire forces based on friction
        F32 surfaceFriction = 1;
        F32 mu = surfaceFriction * (wheel->slipping ? wheel->tire->kineticFriction : wheel->tire->staticFriction);
        F32 Fn = verticalLoad * mu; Fn *= Fn;
        F32 Fw = Fx * Fx + Fy * Fy;
        if (Fw > Fn) 
        {
           F32 K = mSqrt(Fn / Fw);
           Fy *= K;
           Fx *= K;
           wheel->Dy *= K;
           wheel->Dx *= K;
           wheel->slip = 1 - K;
           wheel->slipping = true;
        }
        else 
        {
           wheel->slipping = false;
           wheel->slip = 0;
        }

        // Tire forces act through the tire direction vectors parallel
        // to the surface and are applied at the wheel hub.
        forceVector = (tireX * Fx) + (tireY * Fy);
        pos -= bz * (wheel->spring->length * wheel->extension);
        mRigid.getOriginVector(pos,&r);
        mCross(r, forceVector, &t);
        mRigid.torque += t;
        mRigid.force += forceVector;

I'm still not very good with physic coding and maths... Someone have an idea about what I have to change to unlock free rotation. I want to use something similar to the Hover steering function:

Point3F sn2;
  currTransform.getColumn(0, &sn);
  currTransform.getColumn(1, &sn2);
  mCross(sn, sn2, &r);
  r.normalize();
  torque -= r * (mSteering.x * mDataBlock->steeringForce);

 

Like I said, I'm just starting and will to some experiments but I'm hoping someone can provide me a hint to where I should look at.


What you think prevent the car for steering when I use the Hover steering at end of the WheeledVehicle updateForces code? With some test I have been able to make it work a little but only if the car was moving. (The rotation was faster when the car was moving faster). I tought it was related with the atRest check, but even with atRest always false, it don't rotate. Could enableCollision/DisableCollision be helpfull in my situation?(I think not since the ground collision is deal with castRay).


Any hack suggestions/solution is welcomed! :roll:

Link to post
Share on other sites

After a quick comparaison of Hover and Wheeled updateForces function, I realized the mRigid Torque and Forces are not added the same way. The Hover class use a local variable to add all the forces and add them to mRigid at the end instead of adding different force directly to the mRigid. Maybe I should use the Hover forces update system to prevent my issue? no?

Link to post
Share on other sites

I think you may have already hit on the cause of your issue - the tire code is most likely preventing the body from rotating. Any changes in angular rotation will cause lateral slip on the tires, causing them to generate an opposing force.


Have you tried commenting out the part of the code that applies torque and force from the wheels themselves?

Link to post
Share on other sites
Have you tried commenting out the part of the code that applies torque and force from the wheels themselves?

 

I have not tried to completly comment that part, I will have a try to see if it allow the car to rotate. I didn't tried because I don't want to loose those forces, I understand that it might be the cause but I'm trying to find a way to keep those forces and force car rotation over. I'm about to start a bunch of experimentation and I will try to see what it does to remove the the torque and force of the wheels.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...