Box2d point box towards mouse
in
Contributed Library Questions
•
7 months ago
Hi all, I'm trying to get a (probably dumb) thing to work in PBox2d - to get an arm to point towards the mouse. I have a basic box with a revolute joint and a motor. The rotation works find on it's own.
According to a tutorial I found, I then need to add a mouse joint to direct the motor... but it's not working. My code is pretty lengthy, so perhaps there is someone out there with an example sketch?
Here's my joint definitions:
Ultimately I'd like to implement this with Android so an arm always points down using the accelerometer.
Totally stuck and frustrated - time for a break!
According to a tutorial I found, I then need to add a mouse joint to direct the motor... but it's not working. My code is pretty lengthy, so perhaps there is someone out there with an example sketch?
Here's my joint definitions:
- RevoluteJointDef rev = new RevoluteJointDef();
- Body world = box2d.getGroundBody();
- rev.initialize(body, world, world.getWorldCenter());
- rev.motorSpeed = rotationSpeed;
- rev.enableMotor = true;
- joint = (RevoluteJoint) box2d.world.createJoint(rev);
-
- MouseJointDef md = new MouseJointDef();
- md.bodyA = box2d.getGroundBody();
- md.bodyB = body;
- md.maxForce = 2000;
- mouseJoint = (MouseJoint) box2d.world.createJoint(md);
- Vec2 mouseCoords = box2d.coordPixelsToWorld(mouseX, mouseY);
- mouseJoint.setTarget(mouseCoords);
Ultimately I'd like to implement this with Android so an arm always points down using the accelerometer.
Totally stuck and frustrated - time for a break!
1