Sign in to follow this  

Mouse collision: new Rectangle or move Rectangle?

This topic is 2043 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

Hello, please let me know if I'm being way too nit picky or paranoid about my game's performance with my query:

To see if my mouse intersecting a Rectangle (in XNA), I've thought of two ways:

1. Check if the object's Rectangle intersects with a new Rectangle created at the mouse's position.
Or 2. Initialize a rectangle to follow the mouse around, and update its position every time Update() is called, and when the check occurs, simply provide the mouse's Rectangle.

Assuming the check is called very frequently (whenever other classes want to know if the mouse is hitting anything important), which method would be the most efficient? And, is this difference even significant?

If you'd like to see what I mean in code:

[quote]
//Option #1
class MouseInput
{
[indent=1]MouseState lastState;[/indent]
[indent=1]MouseState currentState;[/indent]

[indent=1]public bool Intersects(Rectangle rect)[/indent]
[indent=1]{[/indent]
[indent=2]return rect.Intersects ( new Rectangle ( currentState.X, currentState.Y, 1, 1) );[/indent]
[indent=1]}[/indent]
[indent=1]public void Update()[/indent]
[indent=1]{[/indent]
[indent=2]lastState = currentState;[/indent]
[indent=2]currentState = Mouse.GetState();[/indent]
[indent=1]}[/indent]
}

//Option #2
class MouseInput
{
[indent=1]MouseState lastState;[/indent]
[indent=1]MouseState currentState;[/indent]
[indent=1]Rectangle mouseRectangle;[/indent]

[indent=1]public void Initialize()[/indent]
[indent=1]{[/indent]
[indent=2]mouseRectangle = new Rectangle ( currentState.X, currentState.Y, 1, 1);[/indent]
[indent=1]}[/indent]

[indent=1]public bool Intersects(Rectangle rect)[/indent]
[indent=1]{[/indent]
[indent=2]return rect.Intersects ( mouseRectangle );[/indent]
[indent=1]}[/indent]

[indent=1]public void Update()[/indent]
[indent=1]{[/indent]
[indent=2]lastState = currentState;[/indent]
[indent=2]currentState = Mouse.GetState();

mouseRectangle.X = currentState.X;[/indent]
[indent=2]mouseRectangle.Y = currentState.Y;[/indent]
[indent=1]}[/indent]
}
[/quote]

Thanks in advance for the help! Edited by genjihl

Share this post


Link to post
Share on other sites
[quote name='genjihl' timestamp='1336713570' post='4939207']
Assuming the check is called very frequently (whenever other classes want to know if the mouse is hitting anything important), which method would be the most efficient? And, is this difference even significant?
[/quote]
In your case it would not really matters if you create or update the rectangle.

But it is best practise to create/delete objects relative seldomly and use an existing object instead, even hold objects in the cache when not needed any longer. Edited by Ashaman73

Share this post


Link to post
Share on other sites
[quote name='laztrezort' timestamp='1336741081' post='4939282']
Doesn't Rectangle have a Contains() method? If so, it would be simpler to use that instead of creating a rectangle of size 1 every time.
[/quote]

You're right, it does. I'll use that instead.

Thanks everyone!

Share this post


Link to post
Share on other sites

This topic is 2043 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this