4
\$\begingroup\$

So the way that I am doing the movement for my game is that you can left click a unit (which is just a rectangle object), and then it is selected. While it is selected, you can right click and that will be the unit's destination.

This is the process input method:

    public void ProcessInput(Vector2 mouseLoc)
    {
        // SO, I have to take this mouse location, which is the location on the screen
        // And convert it to a "world" coordinate

        Vector2 distance = new Vector2(mouseLoc.X - position.X, mouseLoc.Y - position.Y);

        if (distance.Length() < speed)
        {
            position = mouseLoc;
            isMoving = false;
        }
        else
        {
            distance.Normalize();
            Vector2 toMove = new Vector2((int)(distance.X * speed), (int)(distance.Y * speed));
            position += toMove;
            bounds = new BoundingSphere(new Vector3(position, 0), radius);
        }
    }

I have a basic Wall class that is essentially just another rectangle object but with different properties like textures and what not.

This is my check collision method with the wall:

    public Boolean checkCollision(Wall wall)
    {
        if (wall.Bounds.Intersects(this.bounds))
        {
            Vector2 direction = new Vector2((position.X - destination.X) * -1,
            (position.Y - destination.Y) * -1);
            direction.Normalize();
            position += direction * 5;
            destination = position;
            return true;
        }
        return false;
    }

The problem is that the unit just kinda gets stuck in the wall when it hits it. I'm wondering what is the best way to fix this problem. I know that I have to move the unit out of that spot, but I'm not sure how to.

Any thoughts on how to handle this are appreciated.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is this an issue with collision detection or pathfinding? What is the intended behavior when your unit collides with the wall? It seems to me as if your collision is already handled by wall.Bounds.Intersects(this.bounds) \$\endgroup\$ – eclmist Sep 30 '16 at 6:22
  • \$\begingroup\$ Sounds more like collision resolution you are interested in. You have found the collision (detected) you are just not quite handling it right. You could try move first and store the new location in a temporary variable, and then check if the temp collides and if so then don't move the original. \$\endgroup\$ – lozzajp Nov 16 '16 at 10:47
  • \$\begingroup\$ Judging from the code, it is in a 2D plane? If the objects are rectangles, there is no need for a boundingsphere. Rectangle has an intersect method- as a bonus it returns the overlapping rectangle. This provides the infomation you need to know how deep the object penetrates the wall object. \$\endgroup\$ – Felsir Nov 17 '18 at 8:10
0
\$\begingroup\$

I do not know which physics engine you are using but generally , if you want to things go smooth you have two decent ways:

  • Dynamically, use forces to move and let the engine handle the collisions.
  • Kinematically, use your own calculations to move and handle collisions.

If you use kinematics for some of the huge bodies (you have to use kinematics for small bodies like bullets generally) and dynamics for other there will be wrong collision responses.

The answer to your question is generally, make the dynamic object kinematic on collision and move it backwards. When it is not collided anymore make it dynamic again.

\$\endgroup\$

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.