Jump to content
  • Advertisement
Sign in to follow this  
Prot

How to prevent "mouse object" from moving throgh rectangles?

This topic is 1116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

What am I trying to achieve?

I have a Sprite which is supposed to move with the mouse position (kinda like a cursor). In my case though I also have some other Textures (Obstacle-class). So if the mouse collides with such an obstacle I want the texture to stop moving in that direction.

 

What is the problem?

While the texture does follow the mouse and also does stop when the mouse "collides" with an obstacle, at some point the cursor is not within the Bounding Rectangle anymore, but on the other side of a wall for example. The consequence, the texture's position is updated to the mouse position and it suddenly appears behind the wall which is not a desired behavior.

 

My collision method.

        private void CheckCollision(List<Obstacle> _obstacleList, MouseState mState)
        {
            int xOffset = oldMouseState.X - mState.X;
            int yOffset = oldMouseState.Y - mState.Y;

            Vector2 offsetPosition = new Vector2(oldMouseState.X + xOffset,oldMouseState.Y + yOffset);
            
            bool collides = false;

            foreach (Obstacle obstacle in _obstacleList)
            {
                if (obstacle.BoundRectangle.Contains(offsetPosition))
                {
                    collides = true;
                }
            }

            if (!collides)
            {
                position = offsetPosition;
            }
        }

Question

What be a way to prevent the sprite to move through walls in my case?

 

Thanks in advance.

Edited by Prot

Share this post


Link to post
Share on other sites
Advertisement

It looks like you are trying to have the player move one object/sprite move around other objects/sprites (the obstacles) but you have bound the objects position directly to the mouse screen coordinates and each frame it tried to update it's position to where the mouse is.

 

What you need to do is use mouse delta (movement since last frame) instead of absolute coordinates.

 

Therefore (pseudocode)

moveplayer()
{
    var delta = GetMouseDelta();
    
    bool posValid = CheckNewPosition(playerPos, delta);

    if(posValid)
    {
        playerPos += delta; // Move player by delta;
    }
    else
    {
        playerPos = GetClosestPosition(playerPos, delta); // Move player to closest valid position.
    }
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!