BoxWrap2d: Applied Force not moving object
in
Contributed Library Questions
•
2 years ago
I have written some code that has two circles each jointed to a center point (centered circle with density = 0), and a rectangle that is on the floor. I wrote code so that onKeyPress a force would be applied to the rectangle at its center of mass. I can see that clicking the key takes the rectangle out of equilibrium momentarily, but it does not move it, which was my intent. I have tried setting physics.setFriction(0.0); but this has not changed the issue. Can anyone please advise me on what my mistake is? BTW the keys the apply force are 'a' and 'd' every other key resets the sketch.
import org.jbox2d.util.nonconvex.*;
import org.jbox2d.dynamics.contacts.*;
import org.jbox2d.testbed.*;
import org.jbox2d.collision.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.p5.*;
import org.jbox2d.dynamics.*;
Physics physics;
Body crazyMan;
Vec2 force;
Vec2 pointLeft = new Vec2(width, height/2.0);
Vec2 pointRight = new Vec2(0, height/2.0);
float crazyMass;
void setup() {
size(300, 200);
frameRate(60);
smooth();
InitScene();
createObjects();
}
void draw() {
background(255);
}
void mousePressed()
{
// Do something interactive, like creating new objects
}
void keyPressed()
{
switch(key) {
case 'a':
crazyMan.applyForce(pointLeft.mul(crazyMass), pointLeft);
return;
case 'd':
crazyMan.applyForce(pointRight.mul(crazyMass), pointRight);
return;
}
// Can be used to reset the sketch, for example
physics.destroy();
physics = null;
InitScene();
createObjects();
}
void InitScene()
{
// Set up the engine with the sketch's dimensions
physics = new Physics(this, width, height);
}
void createObjects() {
physics.setDensity(0.0);
physics.setFriction(0.0);
Body centerPoint = physics.createCircle(width/2.0, height/2.0, width/60.0);
physics.setDensity(1.0);
crazyMan = physics.createRect(width/20.0, height/6.0, width/10.0, 5.0*height/6.0);
physics.setRestitution(1.0);
Body leftBall = physics.createCircle(width/3.0, height/2.0, width/60.0);
Body middleBall = physics.createCircle(width/2.0, 3.0*height/4.0, width/60.0);
Body rightBall = physics.createCircle(2.0*width/3.0, height/2.0, width/60.0);
DistanceJoint leftJoint = JointUtils.createDistanceJoint(leftBall, centerPoint);
DistanceJoint middleJoint = JointUtils.createDistanceJoint(middleBall, centerPoint);
DistanceJoint rightJoint = JointUtils.createDistanceJoint(rightBall, centerPoint);
leftJoint.setDampingRatio(0.0);
rightJoint.setDampingRatio(0.0);
crazyMass = crazyMan.m_mass;
}
1