Fixed single bone IK with skeleton flipping. Updated SWC for WeightedMeshAttachment.

This commit is contained in:
NathanSweet 2016-02-27 02:12:55 +01:00
parent 3299aa2a06
commit ef0b4b4bb9
7 changed files with 31 additions and 30 deletions

View File

@ -77,10 +77,11 @@ public class IkConstraint implements Updatable {
/** Adjusts the bone rotation so the tip is as close to the target position as possible. The target is specified in the world /** Adjusts the bone rotation so the tip is as close to the target position as possible. The target is specified in the world
* coordinate system. */ * coordinate system. */
static public function apply1 (bone:Bone, targetX:Number, targetY:Number, alpha:Number) : void { static public function apply1 (bone:Bone, targetX:Number, targetY:Number, alpha:Number) : void {
var parentRotation:Number = bone.parent == null ? 0 : bone.parent.worldRotationX; var parentRotation:Number = bone._parent == null ? 0 : bone._parent.worldRotationX;
var rotation:Number = bone.rotation; var rotation:Number = bone.rotation;
var rotationIK:Number = Math.atan2(targetY - bone.worldY, targetX - bone.worldX) * MathUtils.radDeg - parentRotation; var rotationIK:Number = Math.atan2(targetY - bone._worldY, targetX - bone._worldX) * MathUtils.radDeg - parentRotation;
if (bone.worldSignX != bone.worldSignY) rotationIK = 360 - rotationIK; if ((bone._worldSignX != bone._worldSignY) != (bone._skeleton.flipX != (bone._skeleton.flipY != Bone.yDown)))
rotationIK = 360 - rotationIK;
if (rotationIK > 180) rotationIK -= 360; if (rotationIK > 180) rotationIK -= 360;
else if (rotationIK < -180) rotationIK += 360; else if (rotationIK < -180) rotationIK += 360;
bone.updateWorldTransformWith(bone.x, bone.y, rotation + (rotationIK - rotation) * alpha, bone.scaleX, bone.scaleY); bone.updateWorldTransformWith(bone.x, bone.y, rotation + (rotationIK - rotation) * alpha, bone.scaleX, bone.scaleY);
@ -116,16 +117,16 @@ public class IkConstraint implements Updatable {
if (!pp) { if (!pp) {
tx = targetX - px; tx = targetX - px;
ty = targetY - py; ty = targetY - py;
dx = child.worldX - px; dx = child._worldX - px;
dy = child.worldY - py; dy = child._worldY - py;
} else { } else {
var ppa:Number = pp.a, ppb:Number = pp.b, ppc:Number = pp.c, ppd:Number = pp.d; var ppa:Number = pp.a, ppb:Number = pp.b, ppc:Number = pp.c, ppd:Number = pp.d;
var invDet:Number = 1 / (ppa * ppd - ppb * ppc); var invDet:Number = 1 / (ppa * ppd - ppb * ppc);
var wx:Number = pp.worldX, wy:Number = pp.worldY, twx:Number = targetX - wx, twy:Number = targetY - wy; var wx:Number = pp._worldX, wy:Number = pp._worldY, twx:Number = targetX - wx, twy:Number = targetY - wy;
tx = (twx * ppd - twy * ppb) * invDet - px; tx = (twx * ppd - twy * ppb) * invDet - px;
ty = (twy * ppa - twx * ppc) * invDet - py; ty = (twy * ppa - twx * ppc) * invDet - py;
twx = child.worldX - wx; twx = child._worldX - wx;
twy = child.worldY - wy; twy = child._worldY - wy;
dx = (twx * ppd - twy * ppb) * invDet - px; dx = (twx * ppd - twy * ppb) * invDet - px;
dy = (twy * ppa - twx * ppc) * invDet - py; dy = (twy * ppa - twx * ppc) * invDet - py;
} }

View File

@ -38,7 +38,7 @@ import spine.atlas.AtlasRegion;
import spine.attachments.Attachment; import spine.attachments.Attachment;
import spine.attachments.MeshAttachment; import spine.attachments.MeshAttachment;
import spine.attachments.RegionAttachment; import spine.attachments.RegionAttachment;
import spine.attachments.SkinnedMeshAttachment; import spine.attachments.WeightedMeshAttachment;
import starling.core.RenderSupport; import starling.core.RenderSupport;
import starling.display.BlendMode; import starling.display.BlendMode;
@ -146,19 +146,19 @@ public class SkeletonSprite extends DisplayObject {
a = mesh.a; a = mesh.a;
image = mesh.rendererObject as SkeletonImage; image = mesh.rendererObject as SkeletonImage;
if (image == null) mesh.rendererObject = image = SkeletonImage(AtlasRegion(mesh.rendererObject).rendererObject); if (image == null) mesh.rendererObject = image = SkeletonImage(AtlasRegion(mesh.rendererObject).rendererObject);
} else if (attachment is SkinnedMeshAttachment) { } else if (attachment is WeightedMeshAttachment) {
var skinnedMesh:SkinnedMeshAttachment = SkinnedMeshAttachment(attachment); var weightedMesh:WeightedMeshAttachment = WeightedMeshAttachment(attachment);
verticesLength = skinnedMesh.uvs.length; verticesLength = weightedMesh.uvs.length;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength; if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
skinnedMesh.computeWorldVertices(x, y, slot, worldVertices); weightedMesh.computeWorldVertices(x, y, slot, worldVertices);
uvs = skinnedMesh.uvs; uvs = weightedMesh.uvs;
triangles = skinnedMesh.triangles; triangles = weightedMesh.triangles;
r = skinnedMesh.r; r = weightedMesh.r;
g = skinnedMesh.g; g = weightedMesh.g;
b = skinnedMesh.b; b = weightedMesh.b;
a = skinnedMesh.a; a = weightedMesh.a;
image = skinnedMesh.rendererObject as SkeletonImage; image = weightedMesh.rendererObject as SkeletonImage;
if (image == null) skinnedMesh.rendererObject = image = SkeletonImage(AtlasRegion(skinnedMesh.rendererObject).rendererObject); if (image == null) weightedMesh.rendererObject = image = SkeletonImage(AtlasRegion(weightedMesh.rendererObject).rendererObject);
} }
if (image) { if (image) {
a *= skeletonA * slot.a; a *= skeletonA * slot.a;
@ -234,11 +234,11 @@ public class SkeletonSprite extends DisplayObject {
verticesLength = mesh.vertices.length; verticesLength = mesh.vertices.length;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength; if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
mesh.computeWorldVertices(0, 0, slot, worldVertices); mesh.computeWorldVertices(0, 0, slot, worldVertices);
} else if (attachment is SkinnedMeshAttachment) { } else if (attachment is WeightedMeshAttachment) {
var skinnedMesh:SkinnedMeshAttachment = SkinnedMeshAttachment(attachment); var weightedMesh:WeightedMeshAttachment = WeightedMeshAttachment(attachment);
verticesLength = skinnedMesh.uvs.length; verticesLength = weightedMesh.uvs.length;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength; if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
skinnedMesh.computeWorldVertices(0, 0, slot, worldVertices); weightedMesh.computeWorldVertices(0, 0, slot, worldVertices);
} else } else
continue; continue;
for (var ii:int = 0; ii < verticesLength; ii += 2) { for (var ii:int = 0; ii < verticesLength; ii += 2) {

View File

@ -36,7 +36,7 @@ import spine.attachments.AttachmentLoader;
import spine.attachments.BoundingBoxAttachment; import spine.attachments.BoundingBoxAttachment;
import spine.attachments.MeshAttachment; import spine.attachments.MeshAttachment;
import spine.attachments.RegionAttachment; import spine.attachments.RegionAttachment;
import spine.attachments.SkinnedMeshAttachment; import spine.attachments.WeightedMeshAttachment;
import starling.textures.SubTexture; import starling.textures.SubTexture;
import starling.textures.Texture; import starling.textures.Texture;
@ -88,7 +88,7 @@ public class StarlingAtlasAttachmentLoader implements AttachmentLoader {
public function newMeshAttachment (skin:Skin, name:String, path:String) : MeshAttachment { public function newMeshAttachment (skin:Skin, name:String, path:String) : MeshAttachment {
var texture:Texture = atlas.getTexture(path); var texture:Texture = atlas.getTexture(path);
if (texture == null) if (texture == null)
throw new Error("Region not found in Starling atlas: " + path + " (region attachment: " + name + ")"); throw new Error("Region not found in Starling atlas: " + path + " (mesh attachment: " + name + ")");
var attachment:MeshAttachment = new MeshAttachment(name); var attachment:MeshAttachment = new MeshAttachment(name);
attachment.rendererObject = new SkeletonImage(Texture.fromTexture(texture)); // Discard frame. attachment.rendererObject = new SkeletonImage(Texture.fromTexture(texture)); // Discard frame.
var subTexture:SubTexture = texture as SubTexture; var subTexture:SubTexture = texture as SubTexture;
@ -115,11 +115,11 @@ public class StarlingAtlasAttachmentLoader implements AttachmentLoader {
return attachment; return attachment;
} }
public function newSkinnedMeshAttachment (skin:Skin, name:String, path:String) : SkinnedMeshAttachment { public function newWeightedMeshAttachment (skin:Skin, name:String, path:String) : WeightedMeshAttachment {
var texture:Texture = atlas.getTexture(path); var texture:Texture = atlas.getTexture(path);
if (texture == null) if (texture == null)
throw new Error("Region not found in Starling atlas: " + path + " (region attachment: " + name + ")"); throw new Error("Region not found in Starling atlas: " + path + " (weighted mesh attachment: " + name + ")");
var attachment:SkinnedMeshAttachment = new SkinnedMeshAttachment(name); var attachment:WeightedMeshAttachment = new WeightedMeshAttachment(name);
attachment.rendererObject = new SkeletonImage(Texture.fromTexture(texture)); // Discard frame. attachment.rendererObject = new SkeletonImage(Texture.fromTexture(texture)); // Discard frame.
var subTexture:SubTexture = texture as SubTexture; var subTexture:SubTexture = texture as SubTexture;
if (subTexture) { if (subTexture) {