1
\$\begingroup\$

I'm working with MonoGame in VB2015 - please don't give answers that involve using C#.

Anyway, I found a snippet which I converted to VB for pixel collision. The function below accepts two rectangles, then runs a pixel check on the intersection between the two.

What I can't work out is how to send my two sprites (textures) to the function as rectangles. Or if anyone has a alternative, please let me know.

  Private Shared Function IntersectPixels(rectangleA As Rectangle, dataA As Color(), rectangleB As Rectangle, dataB As Color()) As Boolean
    ' Find the bounds of the rectangle intersection
    Dim top As Integer = Math.Max(rectangleA.Top, rectangleB.Top)
    Dim bottom As Integer = Math.Min(rectangleA.Bottom, rectangleB.Bottom)
    Dim left As Integer = Math.Max(rectangleA.Left, rectangleB.Left)
    Dim right As Integer = Math.Min(rectangleA.Right, rectangleB.Right)

(No, this isn't the whole routine...)

\$\endgroup\$
1
\$\begingroup\$

You would need to convert your texture into a rectangle, which is just a position and size.

Your entity that is displaying the texture, should have the position, and the texture itself could potentially hold the size (this is completely dependent on how you are currently doing your spritebatch draw calls).

It would end up looking something like:

Dim textureRect as Rectangle = new Rectangle(CType(position.x, integer), CType(position.x, integer), texture.Width, texture.Height);

If not, can you provide more information about the object that you are trying to create this rectangle from.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.