Character head angles towards cursor?

  • #10, by afrlmeSaturday, 18. October 2014, 17:50 10 years ago
    Hmm the scripts were more as a reference to base a new script on, which showed you how to calculate the angle between 2 points.

    I don't think you would achieve this effect without lua script. On glancing over construct2 I see that there was an option which allows you to simply align assets towards the mouse position. If you wanted to simplify it then you could simply do 2 to 3 states & have the head face left, right or towards the camera based on the x position of the mouse away from the characters current position. You would only require one animation for the head in which you could control the frames based on the position that needs to be set.
    function onMouseEvent(eventType, mousePosition)
      if eventType == eEvtMouseMove then
       if getCursorPos().x < game.CurrentCharacter.CharacterPosition.x - 25 then
        ActiveAnimations["char_head"].AnimationFirstFrame = 1; ActiveAnimations["char_head"].AnimationLastFrame = 1
      elseif getCursorPos().x > game.CurrentCharacter.CharacterPosition.x + 25 then
       ActiveAnimations["char_head"].AnimationFirstFrame = 3; ActiveAnimations["char_head"].AnimationLastFrame = 3
       else
        ActiveAnimations["char_head"].AnimationFirstFrame = 1; ActiveAnimations["char_head"].AnimationLastFrame = 1
       end
     end
    end
     
    registerEventHandler("mouseEvent", "onMouseEvent", {eEvtMouseMove, eEvtMouseLeftButtonDoubleClick, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseLeftButtonHolding, eEvtMouseRightButtonDoubleClick, eEvtMouseRightButtonDown, eEvtMouseRightButtonUp, eEvtMouseMiddleButtonDown, eEvtMouseMiddleButtonUp, eEvtMouseWheelUp, eEvtMouseWheelDown})
    

    ...untested code (again). This would force the animation frame to 1 if mouse position is 25 pixels or more less than current character position or frame 3 if mouse position is 25 pixels or more than current character position else it would use frame 2 which is face looking towards the camera.

    The rotation of the object is a little more complicated I think as the rotation center will be set to the top left pixel of the animation/image by default.

    Also off the top of my head, I'm not sure about the math required to correctly position the head onto the character.

    Something you could do I suppose is create the head as a character & then place the animation center the same as the main character which in theory should place the characters head in the correct position but the character center might be different for each animation so it's a bit hard to calculate.

    Imperator

    7278 Posts


  • #11, by JoemcrSaturday, 18. October 2014, 18:12 10 years ago
    This could be a good solution. Having fewer head positions would look a lot less weird than the unnatural rotation of the head in the Construct2 game as well! Also, I guess if I only made the head move when the character is standing still it could all be part of the standing animation, and a separate head would no longer be necessary(?)

    I will have a play around next week and see what I can do.

    Thanks again for your help,

    Joe

    Newbie

    96 Posts

  • #12, by afrlmeSaturday, 18. October 2014, 18:42 10 years ago
    hmm sure in that case maybe you could create 3 separate idle animations & just switch between them based on mouse position relation to character position & define which animation should be played via the character animation index. Just a thought. Separate object is also possible but positioning & scaling might make things a little complicated.

    Shame we don't have sprite part support.

    & no problem.

    Imperator

    7278 Posts

  • #13, by topineTuesday, 21. October 2014, 07:54 10 years ago
    Have no idea for how calculate it. But if you can rotate an item based in two points, could be possible calculate and fit the same angles to head rotation and cursor position. Just especulating here, maybe there is a formula or something. My geometry is wrost then my english!!

    Newbie

    6 Posts

  • #14, by AlexTuesday, 21. October 2014, 10:22 10 years ago
    there is a compass tutorial which shows an image pointing to the current cursor position:
    http://www.visionaire-studio.net/shop/article/skript-demonst...

    http://home.arcor.de/thoroe/skript_kompass_v1.1.zip

    I think it's related to your problem. It's only in German but maybe it's still useful for you (there are some nice geometry diagrams in the pdf file).

    Great Poster

    378 Posts

  • #15, by afrlmeTuesday, 21. October 2014, 11:55 10 years ago
    I completely forgot about the compass script Alex. If I remember correctly, it used loads of animation frames & the frames were forced based on the mouse position of the current viewpoint.

    Imperator

    7278 Posts