Texture2D.Bounds.Intersect but borders never move? - XNA, .Net 4.0
I'm still brilliantly familiar with XNA, so please forgive any silly questions and statements in this post (an added problem is that I am using Visual Studio 2010 with .Net 4.0, which also means there are very few examples on the internet - well, what i could find easily):
I have two 2D objects in a "game" that I use to learn more about XNA. I need to figure out when these two objects intersect.
I noticed that Texture2D objects have a property named "Bounds", which in turn has an Intersection method that takes a Rectangle (another Texture2D.Bounds) as an argument.
However, when you run the code, the objects always overlap, even if they are on opposite sides of the screen. When I step into the code, I noticed that for Texture2D Bounds I get 4 parameters back when you hover over Bounds and X, and the Y coordinates always read "X = 0, Y = 0" for both objects (hence they always intersect).
I am confused by the fact that the Bounds property is on the texture and not on the position (or Vector2) of the objects. I ended up creating a little helper method that takes objects in there and then calculates if they intersect, but I'm sure there must be a better way.
any suggestions, pointers would be much appreciated.
Gineer
a source to share
I found the tutorials Club XNA Creator based on other posts for stackoverflow Ben the S . The Collision Series 1: 2D Rectangle Collision manual explains it all.
You seem to need to create new rectangles based on the original rectangles that move in the game every time you try to run the intersection method, which will then contain the updated X and Y coordinates.
I'm still not entirely sure why the position of the object's original rectangle can't just be updated, but if that's how it should work, that's good enough for me ... for now .; -)
a source to share
The property has Bounds
been added to the class Texture2D
to make it easier to work with Viewports
. More details here .
You shouldn't think that the texture is the object itself, but simply contains the data that is drawn on the screen, regardless of whether it is used for Sprite
or RenderTarget
. The position of objects or sprites and how the position / movement is handled is entirely up to you, so you'll have to track and handle this yourself. This includes the position of any boundaries.
The 2D Rectangle Collision Tutorial is a good start as you already found :)
a source to share