I want to drag and drop body with its sprite using touch. I do know how to drag and drop sprite, but when I try to move body it… doesn’t work, body stays still. What’s more, sometimes when I touch body the whole app crashes 😡
Can anyone tell me how to drag and drop physic body that collides with other bodies during movement of finger? I’ve been searching for 3 days and im depressed :[
I’ve created 3 similar bodies. One is dynamic(simulation of ball bouncing like in Hockey Game), two of them are kinematic(Will be moveable by players). I will show you declaration of the body I want to move.
I don’t know if any code is needed to show you but I implemented some interfaces found in really bad tutorial. :s
public class MainActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener, IOnAreaTouchListener
Declaration:
//Declaration:
final Sprite face = new Sprite(CAMERA_WIDTH/2+200f, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
final FixtureDef MyFixtureDef = PhysicsFactory.createFixtureDef(0.2f,0.4f,0.6f);
this.scene.registerUpdateHandler(this.mPhysicsWorld);
face.setScale(3);
Body:
final Body facebody = PhysicsFactory.createCircleBody(
this.mPhysicsWorld, face, BodyType.KinematicBody,
CIRCLE_FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
facebody, true, true));
facebody.setUserData("player1");
Methods:
I tried MouseJoint
public MouseJoint createMouseJoint(IAreaShape face, float x, float y) {
final Body boxBody = this.mPhysicsWorld.getPhysicsConnectorManager()
.findBodyByShape(face);
Vector2 v = boxBody.getWorldPoint(new Vector2(x / 32, y / 32));
MouseJointDef mjd = new MouseJointDef();
// mjd.bodyA = ballbody;
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);
}
nor onAreaTouched?
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()) {
final IAreaShape face = (IAreaShape) pTouchArea;
if(this.mMouseJointActive == null) {
//this.mEngine.vibrate(100);
this.mMouseJointActive = this.createMouseJoint(face, pTouchAreaLocalX, pTouchAreaLocalY);
}
return true;
}
return false;
}
here just guessing
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.isActionDown()) {
Debug.d("here!");
return true;
}
return false;
}
}
Question about class:
If I declare class player inside MainActivity:
class Player {
final Body facebody;
final Sprite face = new Sprite(MainActivity.CAMERA_WIDTH / 2+200f,240, MainActivity.mFaceTextureRegion ,getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_MOVE:
// Here 'body' refers to the Body object associated with
// this sprite
facebody.setTransform(pSceneTouchEvent.getX(),
pSceneTouchEvent.getY(), facebody.getAngle());
break;
default:
break;
}
return true;
}
};
Player(Body f) {
facebody = f;
}
}
Everything is perfect but,it yells that player.face can’t be declared in
OnCreateScene in this declaration:
final Player player= new Player( PhysicsFactory.createCircleBody( this.mPhysicsWorld, player.face, BodyType.KinematicBody,CIRCLE_FIXTURE_DEF));
How shall I solve this?
ok finally I found good solution
take it to onCreateScene:
on AreaTouched:
I didn’t make class player, I took it to onCreateScene. And important part of drag and drop is to devide X and Y by
PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT.