How To Use The Mouse Joint In Android?
Solution 1:
i recomend you to see this tutorial example. Copy from it (sorry i don't like broken links :) ) ...
you drag your body with the help of the MouseJoint, it will collide with the other bodys in the world and apply force to them.
Box2d - Manual - http://www.box2d.org/manual.html#_Toc258082974
8.10 Mouse Joint
The mouse joint is used in the testbed to manipulate bodies with the mouse. It attempts to drive a point on a body towards the current position of the cursor. There is no restriction on rotation.
The mouse joint definition has a target point, maximum force, frequency, and damping ratio. The target point initially coincides with the body’s anchor point. The maximum force is used to prevent violent reactions when multiple dynamic bodies interact. You can make this as large as you like. The frequency and damping ratio are used to create a spring/damper effect similar to the distance joint.
Many users have tried to adapt the mouse joint for game play. Users often want to achieve precise positioning and instantaneous response. The mouse joint doesn’t work very well in that context. You may wish to consider using kinematic bodies instead.
So let's start..
- You have to create your PhysicWorld and at least one body in it. ( Checkout the PhysicExample how to.. )
- MouseJoint method
public MouseJoint createMouseJoint(AnimatedSprite box , float x, float y)
{
finalBodyboxBody=this.mPhysicsWorld.getPhysicsConnectorManager().findBodyByShape(box);
Vector2v= boxBody.getWorldPoint(
newVector2(x/pixelToMeteRatio, y/pixelToMeteRatio)
);
MouseJointDefmjd=newMouseJointDef();
mjd.bodyA = groundBody;
mjd.bodyB = boxBody;
mjd.dampingRatio = 0.2f;
mjd.frequencyHz = 30;
mjd.maxForce = (float) (200.0f * boxBody.getMass());
mjd.collideConnected = true;
mjd.target.set(v);
return (MouseJoint) this.mPhysicsWorld.createJoint(mjd);
}
- Touching Body
we have to override our onAreaTouched method to create an MouseJoint anchor-point on the touch position.
MouseJointmjActive=null;
privatefloatpixelToMeteRatio= PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
@OverridepublicbooleanonAreaTouched(
final TouchEvent pSceneTouchEvent,
final ITouchArea pTouchArea ,
finalfloat pTouchAreaLocalX,
finalfloat pTouchAreaLocalY )
{
if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
this.runOnUpdateThread(newRunnable() {
@Overridepublicvoidrun() {
finalAnimatedSpriteface= (AnimatedSprite)pTouchArea; //The touched body//If we have a active MouseJoint, we are just moving arround don't create an 2nd one.if( mjActive == null)
{
Vector2vector=newVector2(pTouchAreaLocalX/pixelToMeteRatio,pTouchAreaLocalY/pixelToMeteRatio);
//=====================================// GROUNDBODY - Used for the MouseJoint//=====================================BodyDefgroundBodyDef=newBodyDef();
groundBodyDef.position.set(vector);
groundBody = mPhysicsWorld.createBody(groundBodyDef);
//====================================// CREATE THE MOUSEJOINT//====================================
mjActive = PhysicsJumpExample.this.createMouseJoint(face, pTouchAreaLocalX, pTouchAreaLocalY);
}
}});
returntrue;
}
returnfalse;
}
- Moving the body
We are moving our finger over the scene, so we have to move the MouseJoint too. If we release the finger.. we must destroy the MouseJoint..
@OverridepublicbooleanonSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
if(this.mPhysicsWorld != null) {
if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_MOVE) {
this.runOnUpdateThread(newRunnable() {
@Overridepublicvoidrun() {
if( mjActive != null ){ //If the MJ is active move it ..// =========================================// MOVE THE MOUSEJOINT WITH THE FINGER..// =========================================Vecotr2vec=newVector2(pSceneTouchEvent.getX()/pixelToMeteRatio, pSceneTouchEvent.getY()/pixelToMeteRatio);
mjActive.setTarget(vec);
}
}});
returntrue;
}
//===========================================// RELEASE THE FINGER FROM THE SCENE..//===========================================if( pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP ||
pSceneTouchEvent.getAction() == MotionEvent.ACTION_CANCEL
) {
this.runOnUpdateThread(newRunnable() {
@Overridepublicvoidrun() {
if( mjActive != null )
{
//======================================// DESTROY OUR MOUSEJOINT//======================================
PhysicsJumpExample.this.mPhysicsWorld.destroyJoint(mjActive);
PhysicsJumpExample.this.mPhysicsWorld.destroyBody(groundBody);
mjActive = null;
}
}});
returntrue;
}
returnfalse;
}
FYI: To fit your needs, you have to play with this settings ( in the createMouseJoint method )
mjd.dampingRatio = 0.2f;mjd.frequencyHz = 30;mjd.maxForce = (float) (200.0f * boxBody.getMass());
Post a Comment for "How To Use The Mouse Joint In Android?"