Jamez0r

A physics update in Unity 2021.2 includes the ability to modify the physics shape at runtime without much/any overhead, using the new PhysicsShapeGroup2D/CustomCollider2D.

I am wondering if this could be used with Spine Bounding Boxes, allowing us to animate the vertices on a boundingbox in Spine, and then at runtime having it update the corresponding physics collider to match the bounding box every frame. If so, it would let us animate hitboxes/hurtboxes which would be awesome!

I posted on the Unity forums, and Melvyn from Unity answered a lot of my initial questions: https://forum.unity.com/threads/2d-physics-in-unity-2021-2.1187764/#post-7631779

From his responses, I think this would be possible and I'm intending to test it out as soon as I can, which may be next week or the week after. I wanted to post here in case anyone had any info or suggestions to add, or would be interested in the code after I test it out.
User avatar
Jamez0r
  • Posts: 321

Harald

Thanks very much @Jamez0r for starting this discussion over at the Unity forum! Looking forward to your insights, any input is of course very welcome as always! 8)
User avatar
Harald

Harri
  • Posts: 3575

Jamez0r

I'm close to having a working 'proof of concept' for this, but I am having an issue with getting the "live" deformed vertex positions for the Bounding Box.

I am referencing the BoundingBoxFollower for the code to get the list of vertices, which uses the Skeleton Extension:
boundingBoxAttachment.GetLocalVertices(slot, buffer);
When I use this, I appear to be getting the vertex positions for the bounding box as it would be in Setup Pose, and not the currently deformed vertex positions (which are deformed in the animation that is currently playing).

Is the boundingBoxAttachment.GetLocalVertices(slot, buffer) the correct way of getting the live deformed vertices? If so, is there a certain time that I need to call it (maybe with a callback)?

Thanks for any help!

Edit: Figured I'd just add my bit of relevant test code (mostly copied from BoundingBoxFollower) in case it helps -
//Have tried calling this in Update() and in LateUpdate()
currentAttachment = slot.Attachment as BoundingBoxAttachment;
Vector2[] boundingBoxVerts = GetBoundingBoxVerticesLocal(slot, currentAttachment);
public static Vector2[] GetBoundingBoxVerticesLocal(Slot slot, BoundingBoxAttachment box, float scale = 1.0f) {
if (box == null) return null;
var verts = box.GetLocalVertices(slot, null);
if (scale != 1.0f) {
for (int i = 0, n = verts.Length; i < n; ++i)
verts[i] *= scale;
}
return verts;
}


---

Poking around in the code a bit, and maybe GetLocalVertices isn't intended to include the deform?

I tried out GetWorldVertices and it looks to be including the deform. One thing I noticed is that the boundingBoxAttachment.WorldVerticesLength value seems to be twice the number of vertices? The bounding box I'm testing this with has 5 vertices, and the WorldVerticesLength is 10.
User avatar
Jamez0r
  • Posts: 321

Harald

Thanks for reporting and sorry for the troubles, you discovered a bug with VertexAttachment.GetLocalVertices here.

A bugfix has just been commited to both 4.0 and 4.1-beta branches, new spine-unity 4.0 and 4.1 are available for download here as usual:
Spine Unity Download

Issue ticket URL for later reference:
https://github.com/EsotericSoftware/spine-runtimes/issues/1990

Regarding WorldVerticesLength: it returns the number of floats in the vertices array (with two floats per vertex), not the number of vertices. Admittedly this is a bit misleading, sorry about that. You can thus see the following line multiple times in MeshGenerator.cs, dividing the float count by two:
attachmentVertexCount = meshAttachment.WorldVerticesLength >> 1;
User avatar
Harald

Harri
  • Posts: 3575

Jamez0r

Harald wrote:Thanks for reporting and sorry for the troubles, you discovered a bug with VertexAttachment.GetLocalVertices here.

A bugfix has just been commited to both 4.0 and 4.1-beta branches, new spine-unity 4.0 and 4.1 are available for download here as usual:
Spine Unity Download

Issue ticket URL for later reference:
https://github.com/EsotericSoftware/spine-runtimes/issues/1990

Regarding WorldVerticesLength: it returns the number of floats in the vertices array (with two floats per vertex), not the number of vertices. Admittedly this is a bit misleading, sorry about that. You can thus see the following line multiple times in MeshGenerator.cs, dividing the float count by two:
attachmentVertexCount = meshAttachment.WorldVerticesLength >> 1;
Thanks Harald!

I have a working proof of concept for this, but haven't tested how long the LibTess function actually takes to process (aka whether or not it would be realistic to use this for a bunch of colliders, or how sparingly it would need to be used). Melvyn on the Unity forum post had also mentioned trying the Ear Clipping algorithm as a method, and also the direct method that if you know your boundingbox polygon is <= 8 sides and is convex that you could just make the PolygonShape2D directly from the boundingbox vertices (without doing any tessellation stuff). Thinking I might just make a drop-down selection box where you can choose which method to use, and then can test out each one's efficiency.

Probably will get to test this out some more this weekend :yes:
User avatar
Jamez0r
  • Posts: 321

Harald

Cool! 8) Looking forward to your results! :)
User avatar
Harald

Harri
  • Posts: 3575


Return to Unity