Stabilizing the vehicle in a racing game

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Stabilizing the vehicle in a racing game

Postby mikeman42 » Tue Feb 22, 2011 8:57 am

I have a problem which is simple enough, but unfortunately I haven't been able to sort it out. Basically, I'm making a podracing game, and I want the vehicles to stay "up" in respect to the track road(which is not always perpendicular to the Y axis though). So, my first method was to find out the angle(or dot product) between the vehicle's Y axis and the floor normal is, and correct it by applying a torque. Something like this:

torque=vehicle.ZAxis()*(Dot(vehicle.YAxis(),normal)-1)

However, this does not seem right when I start to rotate the vehicle around its Y-Axis, for steering. Anyone has any ideas on how the correct code should look like? Thanks!

(anb sorry for double posting this, I picked the wrong forum the first time around)
mikeman42
 
Posts: 19
Joined: Tue May 01, 2007 6:08 pm

Re: Stabilizing the vehicle in a racing game

Postby mikeman42 » Wed Feb 23, 2011 8:45 am

Hm...nobody has anyone idea about how to fix this? It's pretty important for the game....thanks.
mikeman42
 
Posts: 19
Joined: Tue May 01, 2007 6:08 pm

Re: Stabilizing the vehicle in a racing game

Postby ledahut » Wed Feb 23, 2011 8:55 am

However, this does not seem right when I start to rotate the vehicle around its Y-Axis, for steering

What is not right? the steering?, the torque reaction to compensate body rotation with ground?
ledahut
 
Posts: 98
Joined: Mon Jun 21, 2010 8:03 am
Location: France

Re: Stabilizing the vehicle in a racing game

Postby mikeman42 » Wed Feb 23, 2011 9:14 am

I'll try to explain...when I don't rotate(steer) the vehicle at all, then the compensation does work as needed, rotating it only around the z-axis. However, when I start steering, the nose of the vehicle starts pointing upwards. What I would want is the vehicle to be able to rotate(indepedently) around the local z-axis(for compensation) and around the global Y-axis(for steering), but I can't figure out how to do this.
mikeman42
 
Posts: 19
Joined: Tue May 01, 2007 6:08 pm

Re: Stabilizing the vehicle in a racing game

Postby ledahut » Wed Feb 23, 2011 9:53 am

Are you trying to create a car with hinge joint and adding torque to wheels?
Because it is not the good way to do.
ledahut
 
Posts: 98
Joined: Mon Jun 21, 2010 8:03 am
Location: France

Re: Stabilizing the vehicle in a racing game

Postby mikeman42 » Wed Feb 23, 2011 10:07 am

No, the game is in the initial stages right now. The car(hovercraft) right now is just a box, which I steer by rotating in the Y-axis. However, when it collides with walls sometimes it banks on one or the other direction, so I just want to use some torque around the z-axis to compensate and bring it back to balance. It's the combination of rotating in the Y-axis and the compensation that does not work.

So ok, to make things simple, one question: If I have a simple body(a box) and I want it to rotate at the same time:

1)Around the global Y-axis.
2)Around its local Z-axis.

What kind of torque would I need to apply? I think I can figure it out from there.
mikeman42
 
Posts: 19
Joined: Tue May 01, 2007 6:08 pm

Re: Stabilizing the vehicle in a racing game

Postby ledahut » Wed Feb 23, 2011 10:39 am

I think using local force would be better to you:

See this code in pascal from member Delphi
Code: Select all
// retrieves body's velocity at specific point in global space
function GetPointGlobalVelocity(const body: PNewtonBody; Point: Vector): Vector;
var
  velocity, omega: Vector;
begin
  NewtonBodyGetVelocity(body, @velocity);
  NewtonBodyGetOmega(body, @omega);

  Result := AddVectors(velocity, crossProduct(omega, point));
end;

// retrieves body's velocity at specific point in local space
function GetPointLocalVelocity(const body: PNewtonBody; Point: Vector): Vector;
var
  bodymatrix: Tmatrix4F;
begin
  NewtonBodyGetMatrix(body, @bodymatrix);
  Result := GetPointGlobalVelocity(body, MatrixRotateVector(Point, bodymatrix));
  Result := MatrixUNRotateVector(Result, bodymatrix);
end;

// adds force to body at a specified point in global space
procedure AddPointGlobalForce(const body: PNewtonBody; PrevBodyMatrix: pointer; Force, Point: Vector);
var
  GlobalForce: vector;
  bodymatrix:  Tmatrix4F;
  Torque:      vector;
begin

  if PrevBodyMatrix = nil then
    NewtonBodyGetMatrix(body, @bodymatrix)
  else
    move(PrevBodyMatrix^, bodymatrix, sizeof(Tmatrix4F));

  GlobalForce.x := Point.x - bodymatrix[3][0];
  GlobalForce.y := Point.y - bodymatrix[3][1];
  GlobalForce.z := Point.z - bodymatrix[3][2];

  Torque := CrossProduct(GlobalForce, Force);

  NewtonBodyAddForce(body, @Force.x);
  NewtonBodyAddTorque(body, @Torque.x);
end;


// adds force to body at a specified point in local space
procedure AddPointLocalForce(const body: PNewtonBody; Force, Point: Vector);
var
  GlobalForce, GlobalPoint: Vector;
  bodymatrix: Tmatrix4F;
begin
  NewtonBodyGetMatrix(body, @bodymatrix);

  GlobalForce := MatrixRotateVector(Force, bodymatrix);
  GlobalPoint := MatrixTransformVector(Point, bodymatrix);

  AddPointGlobalForce(body, @bodymatrix, GlobalForce, GlobalPoint);
end;


And the explanation at
http://newtondynamics.com/wiki/index.ph ... _engine%3F
(in this page symbol are wrong: / backslash instead of - minus)
ledahut
 
Posts: 98
Joined: Mon Jun 21, 2010 8:03 am
Location: France

Re: Stabilizing the vehicle in a racing game

Postby mikeman42 » Wed Feb 23, 2011 12:17 pm

ledahut, that's a good idea...maybe I was wrong trying to control the vehicle using torque explicitly...I'll try using the article's approach and control it with positioned 'thrusters' instead...thanks!
mikeman42
 
Posts: 19
Joined: Tue May 01, 2007 6:08 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests

cron