Alright, I'm working on a project for school. Were it not for that I'd be doing this in C++. But the teacher uses Java so this project has to be java. I'm extending a program he had written previously to use jBox2D for more realistic physics. It's a top down sumo robot simulator where the students can extend a robot class, write their own algorithm to react to the stimuli in the ring, and then the robot is imported into the simulator and run against other robots.
I've ripped out his old spagetti code and put my own in. I've tried to use the Testbed examples to get this right, tho none of them have objects with constantly changing and potentially alterable thrust except for collisions.
So what I'm trying to do is have each robot have a left and right wheel in the middle of their spherical bodies. They can alter course by altering the drive of the left and right wheels. But when I set everything up the way I think it needs to be I lose contact with the objects once I put them in the world. It seems to me that there's a disjoint between the objects in the world and the variables that I put them in with... am I making sense. maybe I should just show some code:
Now, firs of all, am I right that this creates 2 wheels and attaches them to the body in such a way that when the wheels move they move the body? Also, theoretically I should be able to turn the wheel in the following manner:
(Which coincidentally is the continuation of the code above) and it should move, right? Unfortunately it doesn't.
I'm attaching the rest of the source so that if anyone is inclined to open it up and give me a hand they could. (Also, so no one can say they can't help me without the rest of the code). I'm gonna keep cracking on this, but any insight you more experienced type could offer would be appreciated.
I've ripped out his old spagetti code and put my own in. I've tried to use the Testbed examples to get this right, tho none of them have objects with constantly changing and potentially alterable thrust except for collisions.
So what I'm trying to do is have each robot have a left and right wheel in the middle of their spherical bodies. They can alter course by altering the drive of the left and right wheels. But when I set everything up the way I think it needs to be I lose contact with the objects once I put them in the world. It seems to me that there's a disjoint between the objects in the world and the variables that I put them in with... am I making sense. maybe I should just show some code:
Code:
public class RobotPose {
protected Robot robot;
protected Body body;
protected BodyDef loc;
protected Body leftwheel;
protected Body rightwheel;
public RobotPose(World world, Robot r, float x, float y, float angle) throws Exception {
robot = r;
PolygonDef bodyShape = new PolygonDef();
bodyShape.setAsBox(2f*(float)robot.HALFWIDTH, 2f*(float)robot.HALFWIDTH);
bodyShape.density = 1.0f;
loc = new BodyDef();
bodyShape.friction = .3f;
bodyShape.restitution = 0.65f;
BodyDef lw = new BodyDef();
PolygonDef lwheel = new PolygonDef();
leftwheel = world.createBody(lw);
leftwheel.createShape(lwheel);
lwheel.density = 0f;
lwheel.setAsBox((float)robot.HALFWIDTH / 8, (float)robot.HALFWIDTH);
BodyDef rw = new BodyDef();
PolygonDef rwheel = new PolygonDef();
rightwheel = world.createBody(rw);
rwheel.setAsBox((float)robot.HALFWIDTH / 8, (float)robot.HALFWIDTH);
rwheel.density = 0f;
rightwheel.createShape(rwheel);
loc.angle = (float)(Math.PI * angle/180);
loc.position = new Vec2(x, y);
body = world.createBody(loc);
body.createShape(bodyShape);
RevoluteJointDef leftjoint = new RevoluteJointDef();
leftjoint.initialize(body, leftwheel, new Vec2(-(float)(3 * Robot.HALFWIDTH / 4),0));
leftjoint.enableMotor = true;
leftjoint.maxMotorTorque = 100f;
RevoluteJointDef rightjoint = new RevoluteJointDef();
rightjoint.initialize(body, leftwheel, new Vec2(-(float)(3 * Robot.HALFWIDTH / 4),0));
rightjoint.enableMotor = true;
rightjoint.maxMotorTorque = 100f;
world.createJoint(leftjoint);
world.createJoint(rightjoint);
body.setMassFromShapes();
body.m_mass = (float)robot.getMass(); // Override calculated data
loc.angle = (float)(Math.PI * angle/180);
}
//snippy snip
protected Robot robot;
protected Body body;
protected BodyDef loc;
protected Body leftwheel;
protected Body rightwheel;
public RobotPose(World world, Robot r, float x, float y, float angle) throws Exception {
robot = r;
PolygonDef bodyShape = new PolygonDef();
bodyShape.setAsBox(2f*(float)robot.HALFWIDTH, 2f*(float)robot.HALFWIDTH);
bodyShape.density = 1.0f;
loc = new BodyDef();
bodyShape.friction = .3f;
bodyShape.restitution = 0.65f;
BodyDef lw = new BodyDef();
PolygonDef lwheel = new PolygonDef();
leftwheel = world.createBody(lw);
leftwheel.createShape(lwheel);
lwheel.density = 0f;
lwheel.setAsBox((float)robot.HALFWIDTH / 8, (float)robot.HALFWIDTH);
BodyDef rw = new BodyDef();
PolygonDef rwheel = new PolygonDef();
rightwheel = world.createBody(rw);
rwheel.setAsBox((float)robot.HALFWIDTH / 8, (float)robot.HALFWIDTH);
rwheel.density = 0f;
rightwheel.createShape(rwheel);
loc.angle = (float)(Math.PI * angle/180);
loc.position = new Vec2(x, y);
body = world.createBody(loc);
body.createShape(bodyShape);
RevoluteJointDef leftjoint = new RevoluteJointDef();
leftjoint.initialize(body, leftwheel, new Vec2(-(float)(3 * Robot.HALFWIDTH / 4),0));
leftjoint.enableMotor = true;
leftjoint.maxMotorTorque = 100f;
RevoluteJointDef rightjoint = new RevoluteJointDef();
rightjoint.initialize(body, leftwheel, new Vec2(-(float)(3 * Robot.HALFWIDTH / 4),0));
rightjoint.enableMotor = true;
rightjoint.maxMotorTorque = 100f;
world.createJoint(leftjoint);
world.createJoint(rightjoint);
body.setMassFromShapes();
body.m_mass = (float)robot.getMass(); // Override calculated data
loc.angle = (float)(Math.PI * angle/180);
}
//snippy snip
Now, firs of all, am I right that this creates 2 wheels and attaches them to the body in such a way that when the wheels move they move the body? Also, theoretically I should be able to turn the wheel in the following manner:
Code:
public void run () {
float right = 0, left = 0;
if ((robot.rightWheel == 0) && (robot.leftWheel == 0)) {
left = 2;
right = 1;
} else {
left = (float)robot.getLeftWheel();
right = (float)robot.getRightWheel();
}
leftwheel.setLinearVelocity(new Vec2(0, left));
rightwheel.setLinearVelocity(new Vec2(0, right));
}
}
float right = 0, left = 0;
if ((robot.rightWheel == 0) && (robot.leftWheel == 0)) {
left = 2;
right = 1;
} else {
left = (float)robot.getLeftWheel();
right = (float)robot.getRightWheel();
}
leftwheel.setLinearVelocity(new Vec2(0, left));
rightwheel.setLinearVelocity(new Vec2(0, right));
}
}
(Which coincidentally is the continuation of the code above) and it should move, right? Unfortunately it doesn't.
I'm attaching the rest of the source so that if anyone is inclined to open it up and give me a hand they could. (Also, so no one can say they can't help me without the rest of the code). I'm gonna keep cracking on this, but any insight you more experienced type could offer would be appreciated.