Jump to content

  • Log In with Google      Sign In   
  • Create Account

Mouse collision: new Rectangle or move Rectangle?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 genjihl   Members   -  Reputation: 110

Like
0Likes
Like

Posted 10 May 2012 - 11:19 PM

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
{

MouseState lastState;

MouseState currentState;


public bool Intersects(Rectangle rect)

{

return rect.Intersects ( new Rectangle ( currentState.X, currentState.Y, 1, 1) );

}

public void Update()

{

lastState = currentState;

currentState = Mouse.GetState();

}

}

//Option #2
class MouseInput
{

MouseState lastState;

MouseState currentState;

Rectangle mouseRectangle;


public void Initialize()

{

mouseRectangle = new Rectangle ( currentState.X, currentState.Y, 1, 1);

}


public bool Intersects(Rectangle rect)

{

return rect.Intersects ( mouseRectangle );

}


public void Update()

{

lastState = currentState;

currentState = Mouse.GetState();

mouseRectangle.X = currentState.X;

mouseRectangle.Y = currentState.Y;

}

}


Thanks in advance for the help!

Edited by genjihl, 10 May 2012 - 11:32 PM.


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7991

Like
1Likes
Like

Posted 10 May 2012 - 11:40 PM

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, 10 May 2012 - 11:41 PM.


#3 laztrezort   Members   -  Reputation: 972

Like
1Likes
Like

Posted 11 May 2012 - 06:58 AM

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.

#4 genjihl   Members   -  Reputation: 110

Like
0Likes
Like

Posted 11 May 2012 - 04:04 PM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS