Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:53:29+00:00 2026-06-07T20:53:29+00:00

I want to drag and drop body with its sprite using touch. I do

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:53:30+00:00Added an answer on June 7, 2026 at 8:53 pm

    ok finally I found good solution
    take it to onCreateScene:

            final AnimatedSprite face;
        final Body body;
    
        final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
    
    
            face = new AnimatedSprite(CAMERA_WIDTH/2+200, CAMERA_HEIGHT/2, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
            face.setScale(3);
            body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
    
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
        face.animate(new long[]{200,200}, 0, 1, true);
        face.setUserData(body);
        body.setUserData("player");
        this.mScene.registerTouchArea(face);
        this.mScene.attachChild(face);
    

    on AreaTouched:

        @Override
    public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
    
        if(pSceneTouchEvent.isActionMove()) {
    
            final AnimatedSprite face = (AnimatedSprite) pTouchArea;
            final Body faceBody = (Body)face.getUserData();
            faceBody.setTransform(pSceneTouchEvent.getX() / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, pSceneTouchEvent.getY() / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, faceBody.getAngle());
            return true;
        }
        return false;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to implement a Drag/Drop mechanic in WPF, but it didn't work... With
I want to do a simple drag-drop using jQuery. I have not done anything
Hey I am implementing the drag and drop operations using Kineticjs Now I want
I'm using Eclipse graphical layout, and i want to Drag and Drop components to
I am using VS 2005. I want to drag-and-drop a Word Document 2003/2007 control
I want to let visitors scale the image using click-and-drag: <html> <body> <svg> <image>
I want to drag&drop from treeview to datagrid view. The code for drag is
We're trying to implement drag and drop in Silverlight (3). We want users to
I want to use a drag to select feature, but rather than select a
i want to drag and drop my UIButton from one place to another, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.