• Runtimes
  • Spine - libGDX collision detection

Related Discussions
...

I recently purchased Spine and I would like to use bounding boxes for my collision detection but I’m having trouble figuring out how to use the bounding boxes to do this.

I previously created a working game prototype in libGDX that detects collisions between a man and an object. I created a transparent rectangle over the object and a transparent circle over the man.
manCircle = new Circle();
manCircle.set(x,y,radius);
topSpriteRectangle = new Rectangle(x,y,width,height);

To detect a collision, I have:
if (Intersector.overlaps(manCircle, topSpriteRectangle)) { do something}

This is very simple and works great but now that I purchased Spine, I would like to use bounding boxes for collision detection since these boxes have many vertices and would do a more accurate detection.

I created a Bounding Box in Spine for the man and the Jason file shows:
"head-bb": {
"head": {
"type": "boundingbox",
"vertexCount": 6,
"vertices": [ -19.14, -70.3, 40.8, -118.07, 257.77, -115.62, 285.16, 57.18, 120.77, 164.95, -5.07, 76.95 ]
}
},

I would like to detect a collision between a Spine bounding box and a rectangle and between two Spine bounding boxes. I couldn’t seem to find an easy way to do this without writing my own collision detection methods.

Can you help or point me to an example.

Thanks

First, I would make sure you need to use bounding boxes (polygons) for hit detection. You're right they are more accurate, but that isn't always what you want or need. For example, for something a user clicks for good usability you probably want to use a rectangle or a circle and it's often a larger clickable area than the visuals suggest. Even in many games where hit boxes are extremely important, they still use rectangles. For example, Street Fighter is all rectangles:

Image removed due to the lack of support for HTTPS. | Show Anyway

Anyway, SkeletonBounds is a convenience class that can help by collecting the world vertices for bounding box polygons. It has methods to detect if a point or line segment hits a bounding box polygon, but doesn't have a method for rectangles. You could use SkeletonBounds getPolygons to get the polygons, then use libgdx's Intersector intersectPolygons, passing your rectangle as the second polygon.

SimpleTest2 is an example that uses SkeletonBounds.

Thanks Nate,

Your answer was exactly what I needed. I’ll experiment with getPolygons, but I should probably just use rectangles as you suggested. I believe it would be fine for my app.