IKExample.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #import "IKExample.h"
  30. #import "SpineboyExample.h"
  31. // This example demonstrates how to set the position
  32. // of a bone based on the touch position, which in
  33. // turn will make an IK chain follow that bone
  34. // smoothly.
  35. @implementation IKExample
  36. + (CCScene*) scene {
  37. CCScene *scene = [CCScene node];
  38. [scene addChild:[IKExample node]];
  39. return scene;
  40. }
  41. -(id) init {
  42. self = [super init];
  43. if (!self) return nil;
  44. // Load the Spineboy skeleton and create a SkeletonAnimation node from it
  45. // centered on the screen.
  46. skeletonNode = [SkeletonAnimation skeletonWithFile:@"spineboy-pro.json" atlasFile:@"spineboy.atlas" scale:0.4];
  47. CGSize windowSize = [[CCDirector sharedDirector] viewSize];
  48. [skeletonNode setPosition:ccp(windowSize.width / 2, 20)];
  49. [self addChild:skeletonNode];
  50. self.userInteractionEnabled = YES;
  51. self.contentSize = windowSize;
  52. // Queue the "walk" animation on the first track.
  53. [skeletonNode setAnimationForTrack:0 name:@"walk" loop:YES];
  54. // Queue the "aim" animation on a higher track.
  55. // It consists of a single frame that positions
  56. // the back arm and gun such that they point at
  57. // the "crosshair" bone. By setting this
  58. // animation on a higher track, it overrides
  59. // any changes to the back arm and gun made
  60. // by the walk animation, allowing us to
  61. // mix the two. The mouse position following
  62. // is performed in the lambda below.
  63. [skeletonNode setAnimationForTrack:1 name:@"aim" loop:YES];
  64. // Position the "crosshair" bone at the mouse
  65. // location.
  66. //
  67. // When setting the crosshair bone position
  68. // to the mouse position, we need to translate
  69. // from "skeleton space" to "local bone space".
  70. // Note that the local bone space is calculated
  71. // using the bone's parent worldToLocal() function!
  72. //
  73. // After updating the bone position based on the
  74. // converted mouse location, we call updateWorldTransforms()
  75. // again so the change of the IK target position is
  76. // applied to the rest of the skeleton.
  77. __weak IKExample* scene = self;
  78. skeletonNode.postUpdateWorldTransformsListener = ^(SkeletonAnimation* node) {
  79. if (scene != NULL) {
  80. __strong IKExample* sceneStrong = scene;
  81. spBone* crosshair = [node findBone:@"crosshair"]; // The bone should be cached
  82. float localX = 0, localY = 0;
  83. spBone_worldToLocal(crosshair->parent, sceneStrong->position.x, sceneStrong->position.y, &localX, &localY);
  84. crosshair->x = localX;
  85. crosshair->y = localY;
  86. crosshair->appliedValid = FALSE;
  87. spBone_updateWorldTransform(crosshair);
  88. }
  89. };
  90. return self;
  91. }
  92. #if ( TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR )
  93. - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
  94. position = [skeletonNode convertToNodeSpace:touch.locationInWorld];
  95. printf("%f %f\n", position.x, position.y);
  96. }
  97. - (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
  98. position = [skeletonNode convertToNodeSpace:touch.locationInWorld];
  99. printf("%f %f\n", position.x, position.y);
  100. }
  101. - (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
  102. [[CCDirector sharedDirector] replaceScene:[SpineboyExample scene]];
  103. }
  104. #endif
  105. @end