IKExample.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated April 5, 2025. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2025, 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. #include "IKExample.h"
  30. #include "PhysicsExample.h"
  31. USING_NS_CC;
  32. using namespace spine;
  33. // This example demonstrates how to set the position
  34. // of a bone based on the touch position, which in
  35. // turn will make an IK chain follow that bone
  36. // smoothly.
  37. Scene *IKExample::scene() {
  38. Scene *scene = Scene::create();
  39. scene->addChild(IKExample::create());
  40. return scene;
  41. }
  42. bool IKExample::init() {
  43. if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
  44. // Load the Spineboy skeleton and create a SkeletonAnimation node from it
  45. // centered on the screen.
  46. skeletonNode = SkeletonAnimation::createWithJsonFile("spineboy-pro.json", "spineboy.atlas", 0.6f);
  47. skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
  48. addChild(skeletonNode);
  49. // Queue the "walk" animation on the first track.
  50. skeletonNode->setAnimation(0, "walk", true);
  51. // Queue the "aim" animation on a higher track.
  52. // It consists of a single frame that positions
  53. // the back arm and gun such that they point at
  54. // the "crosshair" bone. By setting this
  55. // animation on a higher track, it overrides
  56. // any changes to the back arm and gun made
  57. // by the walk animation, allowing us to
  58. // mix the two. The mouse position following
  59. // is performed in the lambda below.
  60. skeletonNode->setAnimation(1, "aim", true);
  61. // Next we setup a listener that receives and stores
  62. // the current mouse location. The location is converted
  63. // to the skeleton's coordinate system.
  64. EventListenerMouse *mouseListener = EventListenerMouse::create();
  65. mouseListener->onMouseMove = [this](cocos2d::Event *event) -> void {
  66. // convert the mosue location to the skeleton's coordinate space
  67. // and store it.
  68. EventMouse *mouseEvent = dynamic_cast<EventMouse *>(event);
  69. position = skeletonNode->convertToNodeSpace(mouseEvent->getLocationInView());
  70. };
  71. _eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
  72. // Position the "crosshair" bone at the mouse
  73. // location.
  74. //
  75. // When setting the crosshair bone position
  76. // to the mouse position, we need to translate
  77. // from "skeleton space" to "local bone space".
  78. // Note that the local bone space is calculated
  79. // using the bone's parent worldToLocal() function!
  80. //
  81. // After updating the bone position based on the
  82. // converted mouse location, we call updateWorldTransforms()
  83. // again so the change of the IK target position is
  84. // applied to the rest of the skeleton.
  85. skeletonNode->setPostUpdateWorldTransformsListener([this](SkeletonAnimation *node) -> void {
  86. Bone *crosshair = node->findBone("crosshair");// The bone should be cached
  87. float localX = 0, localY = 0;
  88. crosshair->getParent()->worldToLocal(position.x, position.y, localX, localY);
  89. crosshair->setX(localX);
  90. crosshair->setY(localY);
  91. node->getSkeleton()->updateWorldTransform(spine::Physics_Update);
  92. });
  93. EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
  94. listener->onTouchBegan = [this](Touch *touch, cocos2d::Event *event) -> bool {
  95. Director::getInstance()->replaceScene(PhysicsExample::scene());
  96. return true;
  97. };
  98. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  99. scheduleUpdate();
  100. return true;
  101. }
  102. void IKExample::update(float deltaTime) {
  103. }