Saturday, May 12, 2018

rotation - Bounding Box in Monogame for mouse picking


Hello guys so I'm implementing this mouse manager to be able to pick up objects.


My application uses mainly textured quads in 3d space. I'm using bounding boxes in monogame to do so, however my solution lacks accuracy.



Right now what I have is this: http://imgur.com/oySTCay


you can clearly see what the problem is, so this solution is probably not the best.


this is my method to calculate the bounding box:


        public BoundingBox GetBoundingBox()
{
Matrix transform = Node.GetTransformForBoundingBox();

vertices[0] = Vector3.Transform(new Vector3(0, 0, 0), transform);
vertices[1] = Vector3.Transform(new Vector3(Texture.Width * m_fTexturescale, 0, 0), transform);
vertices[2] = Vector3.Transform(new Vector3(0, (Texture.Height * m_fTexturescale) * -1, 0), transform);

vertices[3] = Vector3.Transform(new Vector3(Texture.Width * m_fTexturescale, (Texture.Height * m_fTexturescale) * -1, 0), transform);

return BoundingBox.CreateFromPoints(vertices);
}

And the mouse picking part is the standard ray tracing into near/far clipping planes with unproject.


So I was thinking, maybe if I do the other way around, and instead of un-projecting the mouse, i could project all the vertices and do some kind of polygon math to check if the mouse position is inside the polygon formed from the projected vertices, (occluded vertices are worrying me though).


Do you think is feasible, or is there any other option, should i just go for OBB?


Thanks alot,


Roger Martins



EDIT:


So after searching a bit for OBB's i found that CartBlanche had one implementation on his samples repo


this: https://github.com/CartBlanche/MonoGame-Samples/blob/master/CollisionSample/BoundingOrientedBox.cs


so I just used it and it works pretty well, now i just have one bug that i cant figure out, one transformation must be wrong.¨


result : http://imgur.com/QOsIbj7


what i'm doing: so now I have a box parameter in this class, nullable one.


how I build the bounding box :


  public SOrientedBoundingBox GetBoundingBox()
{
//create the initial bounding box

if (obb == null)
{
//calculate the vertices for the textured quad in model space at the origin (inferior left corner is origin because we invert the y to conpensate for the perspective projection)
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(Texture.Width * m_fTexturescale, 0, 0);
vertices[2] = new Vector3(0, (Texture.Height * m_fTexturescale) * -1, 0);
vertices[3] = new Vector3(Texture.Width * m_fTexturescale, (Texture.Height * m_fTexturescale) * -1, 0);
//get bounding box and trasnform it to orientedboundingbox
//TODO : remove the middle step
BoundingBox box = BoundingBox.CreateFromPoints(vertices);

obb = SOrientedBoundingBox.CreateFromBoundingBox(box);
return obb.Value;
}
else
{
return obb.Value.Transform(Node.GetQuaternion(), new Vector3(Node.AbsolutePosition.X, Node.AbsolutePosition.Y * -1, Node.AbsolutePosition.Z)*Node.Scale);
}




}

I'm going to fiddle a bit more with it, but the problem is something minor, i think. But maybe one of you can detect something silly i did.


Thanks, Roger.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...