Jump to content
  • Advertisement
Sign in to follow this  
genjihl

Mouse collision: new Rectangle or move Rectangle?

This topic is 2355 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:


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

[indent=1]MouseState currentState;


[indent=1]public bool Intersects(Rectangle rect)

[indent=1]{

[indent=2]return rect.Intersects ( new Rectangle ( currentState.X, currentState.Y, 1, 1) );

[indent=1]}

[indent=1]public void Update()

[indent=1]{

[indent=2]lastState = currentState;

[indent=2]currentState = Mouse.GetState();

[indent=1]}
}

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

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

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

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

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

Thanks in advance for the help! Edited by genjihl

Share this post


Link to post
Share on other sites
Advertisement

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?

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
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.

Share this post


Link to post
Share on other sites

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.


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

Thanks everyone!

Share this post


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

×

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!