I am trying to research the functionality of box2d (using java through libgdx) and and having a rough time getting it to behave as I think it should. I figure I probably don't understand the underlying coordinate systems, but maybe it is something else. In any case, here is the code from a class called Bike which I was building to try to simulate a bike with full suspension. The problem is that I can get the suspension partially working, but it really doesn't behave as it should under most circumstances. I was really trying to use the dynamics approach (apply forces and torques rather than setting positions and angles). In the case of the prismatic joint, it sort of worked to use forces (after having it pointed out that the Vector2 class methods like .sub() and .norm() actually modify the original vector!). Under docile conditions, really low force/movement, the joint seems to work alright. Here is a video.
http://youtu.be/WyYVmt-4ghs
At first I just let it drop due to gravity:
- things seems fine.
Then I give it an upward impulse and let it drop back down (upward force on the center of mass of frame):
- things get weird, why is the lower fork leg being crammed upward?
- the fork leg stays crammed even after releasing the upward impulse force
- when it hits the ground it 'gets' dislodged and behaves like a spring again
Then I give it a big upward impulse, crashing it into the ceiling a couple time:
- once the angle of the body rotates enough, the joint goes insane
- spins around, and looks like it has an angular dependence
The output when printing to console of the forkSpring.getJointSpeed() method is really strange. It seems to return the linear velocity of the whole joint, rather than the relative velocities between the two bodies that the joint connects. The code for the class is below (I have been following several tutorials, so the names/spaces are kinda random). Also, I want to make the swingarm rotate too, but one thing at a time (adding a disjoint swingarm makes it go crazy too):
http://youtu.be/WyYVmt-4ghs
At first I just let it drop due to gravity:
- things seems fine.
Then I give it an upward impulse and let it drop back down (upward force on the center of mass of frame):
- things get weird, why is the lower fork leg being crammed upward?
- the fork leg stays crammed even after releasing the upward impulse force
- when it hits the ground it 'gets' dislodged and behaves like a spring again
Then I give it a big upward impulse, crashing it into the ceiling a couple time:
- once the angle of the body rotates enough, the joint goes insane
- spins around, and looks like it has an angular dependence
The output when printing to console of the forkSpring.getJointSpeed() method is really strange. It seems to return the linear velocity of the whole joint, rather than the relative velocities between the two bodies that the joint connects. The code for the class is below (I have been following several tutorials, so the names/spaces are kinda random). Also, I want to make the swingarm rotate too, but one thing at a time (adding a disjoint swingarm makes it go crazy too):
Code: Select all
package net.obviam.starassault;
import java.util.List;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.PrismaticJoint;
import com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJoint;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
public class Bike {
public Body frame, forkUpper, forkLower, swingarm, frontWheel, rearWheel;
public PrismaticJoint forkSpring; //, rearSpring;
public RevoluteJoint frontAxle, rearPivot, rearAxle;
public int lean, accelerate;
public Vector2 position;
public float forkLength = 2.0f;
public Bike(World world, Vector2 position) {
super();
this.lean = StarAssault.STEER_NONE;
this.accelerate = StarAssault.ACC_NONE;
this.position = position;
// add bike frame
BodyDef bodyDef = new BodyDef();
bodyDef.allowSleep = false;
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(position);
frame = world.createBody(bodyDef);
PolygonShape polyDef = new PolygonShape();
polyDef.setAsBox(1.0f, 0.65f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = polyDef;
fixtureDef.density = 5.0f;
fixtureDef.friction = 2.0f;
fixtureDef.restitution = 0.1f;
// Create our fixture and attach things to the body (frame, forkUpper)
Fixture fixture = frame.createFixture(fixtureDef); // attach frame box
float forkUpperLength = forkLength/2.0f;
float forkUpperWidth = 0.15f;
float forkUpperAngle = (float)(-Math.PI/2.68);
Vector2 forkUpperPos = new Vector2( 1.30f, 0.25f);
forkUpper = world.createBody(bodyDef);
polyDef.setAsBox(forkUpperLength, forkUpperWidth, forkUpperPos, forkUpperAngle);
fixture = frame.createFixture(fixtureDef); // attach front fork upper
// add the lower fork leg //
float forkLowerLength = forkUpperLength;
float forkLowerWidth = forkUpperWidth*0.9f;
float forkLowerAngle = forkUpperAngle;
Vector2 forkLowerPos = new Vector2( (float)(forkUpperPos.x + forkLowerLength*Math.cos(forkUpperAngle)), (float)(forkUpperPos.y + forkLowerLength*Math.sin(forkUpperAngle)));
forkLower = world.createBody(bodyDef);
polyDef.setAsBox(forkLowerLength, forkLowerWidth, forkLowerPos, forkLowerAngle );
fixture = forkLower.createFixture(fixtureDef);
// add prismatic (sliding joint) to constrain lower fork
PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
prismaticJointDef.initialize(frame, forkLower, forkLower.getWorldCenter(), new Vector2((float)(Math.cos(-Math.PI/2.68)), (float)(Math.sin(-Math.PI/2.68))) );
prismaticJointDef.lowerTranslation = -forkLength/1.0f;
prismaticJointDef.upperTranslation = forkLength/1.0f;
prismaticJointDef.enableLimit = true;
prismaticJointDef.enableMotor = true;
prismaticJointDef.collideConnected = false;
forkSpring = (PrismaticJoint) world.createJoint(prismaticJointDef);
// add swingArm to bike
float swingarmLength = 1f;
float swingarmWidth = 0.2f;
float swingarmAngle = (float)(-Math.PI*6.1/7.0);
Vector2 swingarmPos = new Vector2(-1.5f,-0.6f);
swingarm = world.createBody(bodyDef);
polyDef.setAsBox(swingarmLength, swingarmWidth, swingarmPos, swingarmAngle );
fixture = frame.createFixture(fixtureDef);
}
public void update (float deltaTime){
float dx = forkSpring.getJointTranslation();
float dv = forkSpring.getJointSpeed();
Vector2 axis = forkUpper.getPosition().cpy().sub(forkLower.getPosition().cpy()).nor();
float springConstant = 2000f;
float dampingConstant = 160f;
float forceMagnitude = -springConstant*dx-dampingConstant*dv;
Vector2 forceFL = axis.cpy().scl(forceMagnitude);
Vector2 forceFU = axis.cpy().scl(forceMagnitude);
boolean useWorldVector = true;
if (useWorldVector) {
forkLower.applyForce(forkLower.getWorldVector(forceFL.cpy()), forkLower.getPosition().cpy(), true);
forkUpper.applyForce(forkUpper.getWorldVector(forceFU.cpy()), forkUpper.getPosition().cpy(), true);
} else {
forkLower.applyForce(forceFL.cpy(), forkLower.getPosition().cpy(), true);
forkUpper.applyForce(forceFU.cpy(), forkUpper.getPosition().cpy(), true);
}
if (this.accelerate == StarAssault.ACC_ACCELERATE) {
frame.applyForceToCenter(0, 1000, true);
}
if (this.accelerate == StarAssault.ACC_BRAKE) {
frame.applyForceToCenter(0, -1000, true);
}
if (this.lean == StarAssault.STEER_LEFT) {
frame.applyForceToCenter(-1000, 200, true);
}
if (this.lean == StarAssault.STEER_RIGHT) {
frame.applyForceToCenter(1000, 200, true);
}
forkLower.setTransform(forkLower.getPosition(), forkUpper.getAngle()); // force lower and upper fork to have same angle ... I wish there was a better way
System.out.println("dv: "+dv+" acc: "+this.accelerate+" forceFU: "+forceFU+" forceFL: "+forceFL+" forceMagnitude: "+forceMagnitude+" axis: "+axis+" spring: "+forkSpring.getJointTranslation());
}
}