Make a spring Body

  ... the Physics Box2D Extension for AndEngine.

Make a spring Body

Postby Zeeshan Abid » Tue Mar 06, 2012 8:48 am

I am new in box2d and also in andengine. I want to create a spring body. I have used to joints revolute joint and mouse joint. Actually my requirement is that if I release the body of moving the object. It should give me an effect of spring which moves in limits. For example if the board is jointed with a pole and i drag it down ward and release it then it gives a dumping effect and then stay on its actual position. I have made two bodies and I can drag it but it does not stop after releasing and also does not stay also it actual position. Please guide me and try to help me. Thanks you so much...

Revolute Joint...

final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(anchorBody, movingBody, new Vector2(1, movingBody.getPosition().y));
revoluteJointDef.collideConnected = false;
revoluteJointDef.enableMotor = false;
revoluteJointDef.motorSpeed = 0;
revoluteJointDef.maxMotorTorque = 0;

revoluteJointDef.enableLimit = true;
revoluteJointDef.lowerAngle = MathUtils.degToRad(-30);
revoluteJointDef.upperAngle = MathUtils.degToRad(30);

mPhysicsWorld.createJoint(revoluteJointDef);

Mouse Joint...

final Body body = (Body) pFace.getUserData();
final MouseJointDef mouseJointDef = new MouseJointDef();

final Vector2 localPoint = Vector2Pool.obtain((pTouchAreaLocalX - pFace.getWidth() * 0.5f) / physicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, (pTouchAreaLocalY - pFace.getHeight() * 0.5f) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);

mGroundBody.setTransform(localPoint, 0);

mouseJointDef.bodyA = mGroundBody;
mouseJointDef.bodyB = body;
mouseJointDef.dampingRatio = 1.0f;
mouseJointDef.frequencyHz = 30;
mouseJointDef.maxForce = 200.0f * body.getMass();
mouseJointDef.collideConnected = true;

mouseJointDef.target.set(body.getWorldPoint(localPoint));
Vector2Pool.recycle(localPoint);

return (MouseJoint) mPhysicsWorld.createJoint(mouseJointDef);
Zeeshan Abid
 
Posts: 2
Joined: Fri Mar 02, 2012 1:14 pm

Re: Make a spring Body

Postby Zeeshan Abid » Thu Mar 08, 2012 6:48 am

Any one is hearing?
Zeeshan Abid
 
Posts: 2
Joined: Fri Mar 02, 2012 1:14 pm

Re: Make a spring Body

Postby yatuor » Thu Mar 08, 2012 10:39 am

try prismatic joint. make a fake dock body which your spring will joint with. change the sprite size according to the sprite position change.
not sure if this is what u want.

u may apply a linear velocity(or apply force) in updater if u want it to move back after dragging
yatuor
 
Posts: 10
Joined: Sat Aug 06, 2011 10:23 am


Return to Physics Box2D

Who is online

Users browsing this forum: No registered users and 12 guests