Smooth Character turning / Natürlich Charakterdrehung

  • #40, von afrlmeFreitag, 03. Oktober 2014, 18:13 Uhr vor 10 Jahren
    The math equations in the script are not perfect, hence it doesn't always take you the shortest way from current direction to the next direction. I don't think there were any typos in his script as I got it working.

    I have spent the past 2 days working on equations to correctly get the shortest rotation route from current direction to target direction. I am not willing to share it yet as I've not gotten round to writing the part of the script that handles character states or destination actions. Maybe I will get round to it later this week as my head is still hurting from all the different math equations & methods I tried using. & FYI I'm not right good at mathematics, as it takes me about 10 times longer than average person to calculate simple math stuff like multiplications & divide etc. Simple stuff. confuse

    Foren Imperator

    7285 Posts


  • #41, von Lord7evenFreitag, 03. Oktober 2014, 23:45 Uhr vor 10 Jahren
    I've got a 98% working solution but it's a bit ugly and on long walking distances with many turns it shows a wrong frame for about 3ms after the turn is complete and before the character walks to the next waypoint. I guess there's something wrong in the order of my code so I had to make some workarounds. Maybe it helps a little or gives some other ideas for brighter brains than me. I've made a little demo so you can see what's going on.

    https://www.youtube.com/watch?v=IC0FmZuE_g4

    Neuling

    40 Posts

  • #42, von afrlmeSamstag, 04. Oktober 2014, 00:20 Uhr vor 10 Jahren
    hmm very strange that it turns in the opposite direction once before turning the correct way.

    In the method I am working on I have only used approx 30 lines of code so far & mine gets the range values of the base & target directions & then it calculates shortest route to next target by adding or subtracting 45 as many times as needed from the base range until it gets to the targets range value. It also checks if current table entry is 0 then next value is 360 - 45 else if turning other way then if value is 325 then next value will be 0 etc. This way we generate the perfect angles based on the direction compass.

    I am also planning on sorting the animations different. Instead of using one animation we will use an animation for each direction which will contain at least 2 frames in each one, a start turn animation frame & a turning frame. We can force which frames will be played using the AnimationFrameFirst & AnimationFrameLast data tables, & in this way we can potentially create a smooth begin rotation, rotation & end rotation animation.

    Also based on this I believe it would be possible to create a begin & end walk animations that can be played before setting off & at destination. Apart from the rotation part, this is all pure theory that I have yet to test.

    Foren Imperator

    7285 Posts

  • #43, von PykeSamstag, 04. Oktober 2014, 10:16 Uhr vor 10 Jahren
    Looking awesome guys. If you crack it Ill certainly be implementing it in Stasis!

    Neuling

    59 Posts

  • #44, von Lord7evenSamstag, 04. Oktober 2014, 10:37 Uhr vor 10 Jahren
    Wow AFRLme! That sounds very nice!
    My solution calculates the fastest turn with simple if else statements based on whether the target direction lies between the start direction and the start direction +/- 180° (+180° if the start direction is smaller than 180° and -180° if the start direction is bigger than 180°):
    if startDirection == 0 then startDirection = 360 end
    if endDirection == 0 then endDirection = 360 end
    
    if startDirection > 180 then
      if endDirection > startDirection - 180 and endDirection < startDirection then
        return true
      else
        return false
      end
    else
      if endDirection > startDirection + 180 or endDirection < startDirection then
        return true
      else
        return false
      end
    end
    

    if this code returns true, it should turn the character clockwise. With a false return, it should turn the character anti-clockwise ergo play the turn animation backwards.

    With the script-part from SimonS I get back the correct frame for the start & target direction and this is now always the shortest turn. It also gives a little randomness to 180° turns since it calculates the turns based on real directions.

    I forgot to mention that there is a problem with that "one animation for the whole turn" solution. It seams that Visionaire don't play the animation from the last frame to the first one. If it reaches the real last frame it stops and don't continue with the first one. (Like first frame of the turn is 4 and last frame is 1 playing forward). For this case I added another 360° turn to the animation and set the start and/or end frame to what it is + the numbers of frames in a whole 360° turn.

    Tested it with the demo above (45° steps) and also with 90° steps but it should work with any amount of steps. If we could manage it to set different animations for each turn, it would simplify the animation handling part in the script and it then could be a working solution.

    I'll see what I can do but for now I need some weekend. ;-)
    Thanks again for all the input!

    Neuling

    40 Posts

  • #45, von Lord7evenMontag, 06. Oktober 2014, 11:23 Uhr vor 10 Jahren
    I've modified my code so that there are no workarounds anymore. I have now about 56 animations, one for each turn till 180°. Now I don't need a dummy first frame because I don't have to set first & last frame or set animations to play backwards in the script. It gives also the ability to set different animations for each turn which is kind of nice (but hard work to set up).

    Somehow the strange bug of showing a wrong frame after a turn is still there. This happens when the character has more than one turn to get to his destination. After recording and looking at it frame by frame, I saw that after the turn, the character is looking at the point where the user clicked instead of looking at the direction to go next. This is because SimonS script sets destination but the engine then sets other direction because of the waysystem and waypoints. (When there is no way to go directly to the clicked point).

    I guess this will take me a long time to fix since I'm not that into the scripting part of Visionaire. Took me a whole while to figure out what’s going on in SimonS script too, but I'm working on it. ;-)

    Neuling

    40 Posts

  • #46, von afrlmeMontag, 06. Oktober 2014, 13:16 Uhr vor 10 Jahren
    Sounds a bit overkill the me. The more animations/frames you add/create, the more resources/power is needed to run the game.

    What I was talking about was: if you are using 8 directions, then you need 8 animations of about 2-3 frames each.

    Frame 1 = initial lift foot off floor frame
    Frame 2 = the frame that will be used while rotating.
    Frame 3 = same as frame 1 ( if character destination = empty [ rotate only ] )

    Alternatively you could use another outfit & have one that sets off walking from left leg & one that sets off walking on right leg & that way you could make the walk/rotation even more realistic by having it pivot on the correct leg.

    I'm not sure why Simon's script is triggering another direction before rotating the correct way. Simon scripts a lot different to me. I tend to try & keep scripts small & compact by using the least amount of code & lines as possible. Plus I tend to add comments to my code (mostly for myself, so that I can remember what everything does) whereas Simon doesn't bother with comments much, which means you have to go through each line of code trying to figure out what does what. Each scripter/programmer works differently & has their own workflow.

    Foren Imperator

    7285 Posts

  • #47, von Lord7evenMontag, 06. Oktober 2014, 14:15 Uhr vor 10 Jahren
    Well I was first trying to use only the standing animations and do a loop until the character stands in the right direction. (Kind of like AFRLmes idea/solution.) But I couldn't get it to work. The problem was to play 2 animations before walking, but I guess this could be done and then we could do something like you talk about, which would be very awesome! I just wanted to isolate the "show a wrong frame at the end of the turn" problem with my solution. For me this would be a no-go anyway because of the 56 animations only for the turns. (If I could correctly play an animation backwards with the script, it would only need 28 animations but this is still too much.)

    Ya, I also tend to write comments in the code, I just didn't because the solution I've uploaded is not a finish one and Simon also said that his code is just an idea. Anyway I learned a little bit by trying to understand his code. I guess the direction or the destination is set, so the character don't move in any direction while he's turning. I've commented these lines out one by one and then it does some weird stuff like the character floats to the destination while turning or it plays the wrong walking animation after the turn.

    I just don't know exactly why the character is turned to a wrong direction after the turn or how I can fix that. After it's done I would make the code smaller. I first try to make things work and then do the cleaning job. ;-) I also try to keep things "open", so it will work with all amount of directions, from 4 (or 2) to n. (Something I've learned by coding my website content management system.)

    Neuling

    40 Posts

  • #48, von afrlmeMontag, 06. Oktober 2014, 15:10 Uhr vor 10 Jahren
    I wouldn't bother with turning under 8 directions. Bit pointless really.

    8 directions in VS is ok, 12 is possible, 16 is also possible but I wouldn't recommend it unless your character sprites are fairly small. Stasis has 16 directions, but because of the isometric graphics, it means that his character is always pretty small.

    From what I gather & what Simon said to me: His script listens out for when character changes from idle to walk & for when the direction changes, so when character can be rotated it stores destination/commands & then stops the character, calculates the shortest distance, rotates the character & then restores the destination/commands, thus making the character start walking again. The small glitch could be caused by the character not stopping fast enough before new walking direction is set... But again, I'm not sure.

    Foren Imperator

    7285 Posts

  • #49, von Lord7evenMontag, 06. Oktober 2014, 20:15 Uhr vor 10 Jahren
    Turning under 8 directions makes sens for my game because I have only 4 walking-direction animations, so it would be great to at least turn the character to front before to the left/right (in a 180° turn). I also could add 45° steps to the turns without having 8 walking or standing animations and I guess it would also make sens with only 2 directions, if you make a simple 2d game without vanishing points and so. Maybe there was some misunderstanding. In the turning animation it has to be of course 8 directions or more so it looks smooth.

    Neuling

    40 Posts

  • #50, von afrlmeMontag, 06. Oktober 2014, 20:55 Uhr vor 10 Jahren
    Ah yeah I misunderstood what you meant. I know that quite a few people on here are making side-scrolling adventure games & thus don't need as many walking directions.

    I was thinking more about generating amount of degrees based on amount of walk directions.

    With 2 walk directions there wouldn't be much point in calculating shortest distance really. You might as well just create 2 animations for that - well actually even 1 would work as you would be able to play it forwards or in reverse. You would just need to store destination/actions inside of a table, stop character, play animation & then restore the destination/actions.

    Foren Imperator

    7285 Posts