Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Rungo73

Hi,

I'm building a top down racer and I have a car that travels around ok, it's just that's its on rails. Need some clues as to how I could implement 'drifting' on corners to give the car some fun.

Can anyone show me some code/ maths/ tips?

I did find some tutorials that showed box2d with four wheels but I'm after an implementation for a single entity.

Cheers,

1 decade ago by Joncom

I would recommend seeing what you can extract from this tutorial. The car has 4 wheels, but has the advantage of letting Box2D handle the physics. Otherwise you'd be writing physics to make a single body behave like many. It's also possible to make a single entity that has multiple bodies.

1 decade ago by Rungo73

Thanks for this - looks promising. Appreciated.

1 decade ago by Rungo73

@Joncom

I'm having trouble porting the code in the top of the tutorial you suggested. Not sure about a couple of things and was hoping you could point me in the right direction.

Im using your easy box2d plugin.

For instance in this function..

 b2Vec2 getLateralVelocity() {
      b2Vec2 currentRightNormal = m_body->GetWorldVector( b2Vec2(1,0) );
      return b2Dot( currentRightNormal, m_body->GetLinearVelocity() ) * currentRightNormal;
  }

I've rewritten as..


 getLateralVelocity: function() {
      
      
    // this line isnt correct. how do I tell this.cuurentRightNormal to be a b2Vec2? as in //code above?
      this.currentRightNormal = new Box2D.Common.Math.b2Vec2();

      this.body.GetWorldVector( new Box2D.Common.Math.b2Vec2(1,0) );

      return  Box2D.Common.Math. b2Mat22.FromVV( this.currentRightNormal, this.body.GetLinearVelocity() ) * this.currentRightNormal;
      
  },


Also, with your plugin do I have to give my body and definitions, ie shape, mass etc?

My goal is to model a rectangle as a car (not worried about the wheels) and apply some physics to it so I can slide and drift it around like @enpu 's minikart game.

Any help would be very much appreciated.

1 decade ago by Joncom

Quote from Rungo73
how do I tell this.cuurentRightNormal to be a b2Vec2? as in //code above?
I would translate the line:
b2Vec2 currentRightNormal = m_body->GetWorldVector( b2Vec2(1,0) );

...like this:
var vector = new Box2D.Common.Math.b2Vec2(1, 0);
// GetWorldVector returns a b2Vec, thus currentRightNormal will be one.
var currentRightNormal = this.body.GetWorldVector(vector);

However, it doesn&039;t look like this version of Box2D includes the #b2Dot function, but maybe your found a work-around already?

Edit:
Also, with your plugin do I have to give my body and definitions, ie shape, mass etc?
Mass is automatically calculated by Box2D when you create the entity, based on shape, size, and density.

You can define the shape of an entity by extending the corresponding entity:
- entity.js for a rectangle.
- entities/capsule.js for a pill shape.
- entities/circle.js for a circle.
- entities/polygon.js for other custom shapes.

The size of the entity is usually determined by entity.size.x and entity.size.y. In the case of a circle though, it&039;s #entity.radius.

The value of entity.density is used when creating the body during initialization.

1 decade ago by Rungo73

I think I can use Box2D.Common.Math.FromVV() for b2Dot.

1 decade ago by Krawaller

The Dot function is here: Box2D.Common.Math.b2Math.Dot

1 decade ago by Krawaller

Here's my full version of GetLateralVelocity. I changed it to directly calculate the needed counterforce, so this function returns the vector you want to apply to the body.

getLateralCounterForce: function(){
    var vector = new Box2D.Common.Math.b2Vec2(0,1);
    var currentRightNormal = this.body.GetWorldVector(vector);
    var linearVel = this.body.GetLinearVelocity();
    var dot = Box2D.Common.Math.b2Math.Dot(currentRightNormal,linearVel);
    currentRightNormal.Multiply(dot*this.body.GetMass()*-1);
    return currentRightNormal;
}

1 decade ago by Joncom

Quote from Krawaller
The Dot function is here: Box2D.Common.Math.b2Math.Dot
Oh, cool, thanks!
Page 1 of 1
« first « previous next › last »