1
\$\begingroup\$

I'm trying to have a body move to the player touch and stop once he reaches the destination. I have the player boxed in to prevent him to move out of bounds, I can somewhat do that with translate but with collision, it doesn't work properly, is there a better solution for this?

\$\endgroup\$
  • 1
    \$\begingroup\$ Needs more detail. Is your game side-view or top-down? Is there gravity, or friction, or other objects in the way? \$\endgroup\$ – Anko Sep 6 '16 at 15:19
  • 1
    \$\begingroup\$ I agree with Anko, there's too little detail here. Anytime you say "it doesn't work" you absolutely must describe in what specific way does it not work? This could be anything from "the body hitches or stutters in its movement" to "the body moves to the wrong place" to "the body does nothing" to "the body disappears" to "the game crashes and my computer catches fire" - so you see why we could use some help narrowing down the problem. Remember that none of us has ever seen your game, so something that may seem obvious to you won't be clear to us unless you include it in your question. \$\endgroup\$ – DMGregory Sep 17 '16 at 16:25
  • \$\begingroup\$ Have you considered using a mouse joint? I think it could do the job depending on exactly what you mean. \$\endgroup\$ – Louis Langholtz Aug 13 '17 at 3:53
2
\$\begingroup\$

Once you have the destination determined, it can be done in two popular ways:

  • Tweening. That is the most simple way of transforming an object to a destination in exact N seconds with the ease algorithm of your choice. Most tween engines has path feature to allow you to put checkpoints on their way, so you can take a longer path.

  • Physics. You can control your player navigation code to help it reach to the destination and continue to control or apply force (in case there is a friction). Finally, once arrived (collided), remove all forces/controls.

If your scene is complicated and your player has to make decisions on the road, then AI is your friend.

\$\endgroup\$
1
\$\begingroup\$

First, you need to define a bounding box around your player. This is what actually collides with the outside. To try to calculate collisions with every pixel of your character, your CPU would put you in the realm of terrible FPS. Since you are programming in Java, I would recommend looking into the java.awt.Rectangle class. You can simply define one rectangle as your "out of bounds box" and one rectangle around your player, and use outOfBoundsRectangle.contains(playerRectangle) to check if the player is in bounds.

What if your bounds isn't a simple rectangle?

The "contains method can also apply to single points. Just if your player's bounding box contains any points in your out of bounds shape.

What if that is taking too long/too much processing power

If you have a lot of points in your out of bounds shape, the above method will be too tedious. You can try dividing up a larger shape into smaller rectangles that can be checked for collisions.

The best way for working with a complicated boundary is by making it into a java.awt.geom.Path2D - a class that defines a set of points with lines between them. Using methods in this class, you can check for intersections between the path and your player's bounding box. You could also create a bounding path for your player for more complicated collisions.

Edit: I'm not sure whether you are writing code for 2D or 3D. What I gave you above only works in two dimensions. For three dimensions, I believe libgdx has a BoundingBox class that works in a similar way to what I described above. It can also replace the Rectangle class if you define the BoundingBox as only having two dimensions. The BoundingBox is defined by two "Vector3"s, another libgdx class that just stores a 3D vector. Again, you can make this 2D by setting one of the vector dimensions to 0.

As for clicking and having your player move to that point, that should be done by frames in your update method. I'd recommend looking at this site for some easing equations, but a linear equation will work fine for a basic game:

Some easing equations

\$\endgroup\$
  • \$\begingroup\$ Interesting answer but I am not sure it answered OP's question \$\endgroup\$ – realUser404 Oct 21 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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