Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am making a program that pick, drag, and drop the ball. In real life, once you pick the ball and drop it, whenever the available object is (what you see is what you get) will only be picked and dropped again and again. However, in the game program, there is something that at first you did pick and drop the ball. When you did the second time, something hit like some sort of an "invisible object" and the physics frame (in pink) is replicating while the frame in green is the wall and the floor. Take a look at these pictures.

Before picking up and drop a ball for the first time... enter image description here

When the ball in dropping... (Showing in pink frame for physics frame) enter image description here

When picking up the second time, the previous pink physics frame did not come off and ended hitting something invisible. enter image description here

So tell me, did I made something wrong in this touchDown() code or not? I have observed something here after I run in debug mode.

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) 
{
    // TODO touchDragged

    float change_x = (Gdx.input.getX()/(cv.getPhysicsCamera().viewportWidth))*5000;
    float change_y = (Gdx.input.getY()/(cv.getPhysicsCamera().viewportHeight))*15;

    x = ((change_x/cv.getPhysicsCamera().viewportWidth)/2) - (rc.getSoccerBallWidth()/2);
    y = (((cv.getPhysicsCamera().viewportHeight) - (change_y/2)) - (rc.getSoccerBallWidth()/2));

    Vector3 touch_point = new Vector3(change_x, change_y, 0);
    cv.getPhysicsCamera().unproject(touch_point);

    // Set new body frame.
    ballDef.type = BodyType.DynamicBody;
    ballDef.position.set(x, y);

    ballBody = wc.getWorld().createBody(ballDef);
    ballBody.setActive(false); // --> Allows you to display a cursor for dragging without letting your soccer ball to fall off while holding.

    return true;
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Ideally, you'd assign your ball a physics body and update that instead of creating a new physics body everytime the user picks up the ball.

E.g. call wc.getWorld().createBody(ballDef); only once when you init the game and only move it to the position of the ball.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.