The service is unavailable.

Rotate sprite while jumping

Get help using Construct 2

Post » Fri Sep 07, 2012 11:46 am

Working on a Platform game, I have the Player Sprite.
What I want to do, is rotate it while it jumps, then bring it back to 0 when it reaches the ground.

I almost got it to work, but the problem is that when it reaches the ground it "shakes" a bit until it settles down. Any idea what I'm doing wrong?

evyatron2012-09-07 11:46:52
Posts: 9
Reputation: 293

Post » Fri Sep 07, 2012 12:45 pm

Don't use the same sprite for rotation and platforming. Because of collision issue, when your sprite is rotated 15 degree one corner will touch the ground.
Then you set it to 0 degree it goes back in fall mode. Probably creating some wiggling.
You should then do something like setting another sprite to follow your platform sprite. I usually set the platform behavior on my collision box only and then set the eye candy graphics of the character on another sprite that doesn't handle collision.
Let's call it "PlayerSkin"
(You'll have to put your mirroring logic to the PlayerSkin instead by the way)

[code]Global number JRotSpeed = 120 // in degree per second
Global number JAngleLimit = 15 //Limit of jump rotation angle

Player: On Jump
-> PlayerSkin: set rotSpeed to PlayerSkin.isMirrored ? JRotSpeed : -JRotSpeed

Player: On Fall
-> PlayerSkin: set rotSpeed to PlayerSkin.isMirrored ? -JRotSpeed : JRotSpeed

Player: On Landed
-> PlayerSkin: set rotSpeed to 0
-> Player: set angle to 0

Every Ticks:
-> PlayerSkin: Set Angle to Clamp(Player.angle+rotSpeed*dt,-JAngle,JAngle)[/code]

(Sorry, didn't keep your awesomely long variable names :D)
Use dt, this way your movement isn't fps dependant
if you rotate 2 degree per frame the movement won't have the same speed at 30 and 60fps
By multiplying by dt (time delta = time (in seconds) between the previous and the current frame) you'll always keep the same speed.
And also it allows you to express your values per seconds (adding 1*dt to a value each frame, this value will go up to 1 per second)
Anyway it should workYann2012-09-07 12:47:30
Posts: 1,481
Reputation: 16,400

Post » Fri Sep 07, 2012 1:59 pm

Thanks for the info!

I have to admit I didn't FULLY grasp the concept of the dt, but I'm going with you here. This is what I have now, however it's still not working properly! Now when I jump it jitters :)
If I remove the Clamp then it doesn't jitter, but it goes above the 15 degrees (obviously).

Also note (don't know if it was clear from my previous screenshot)- when the play falls down I want it to be in he opposite of that degrees. I mean that if when jumping I make it go to -15, then when it falls it should be 15, and then settle on 0 when it hits the ground.

So here's the updated Events Sheet, thanks for the help!
Posts: 9
Reputation: 293

Post » Fri Sep 07, 2012 7:06 pm

I did using frames, because after many tries, my version was not good enough. But using frame animations, it's perfect.

Also, I was having several issues with the detection, also when pinning the animation to a invisible player.

The sandBOX was made with these rotating frames, you can check them, and you'll see what I mean of feeling good, because the animation always will show the right frame on the right moment of the rotation, instead of depending from forces, torques, etc.
Posts: 1,337
Reputation: 20,975

Post » Fri Sep 07, 2012 8:08 pm

Ok how I ended up doing this (and now it works great) is this:

- define a new RotateTo instance variable
PlayerBox on Platform Jump

PlayerBox on Platform Fall

PlayerBox on Platform Landed
    Set Player.RotateTo = 0
    Set Player.Angle = 0

    Rotate Player PLAYER_JUMP_ROTATE_SPEED*dt toward Player.RotateTo

Thanks for the help!
Posts: 9
Reputation: 293

Post » Sat Sep 08, 2012 5:36 pm

Oh right, I think the jittering is caused by the fact that when you set angles to a negative value, c2 converts it to a 0-360 range.
so, as soon as you rotate counter clockwise past 0 degree it will go to 360 then it's clamped to 15...
So basically you have a sequence of values like that (if isMirrored is true and you have a steady 60fps which gives you a 1/60=0.017 for the value of dt) :
frame 1: 0
frame 2: clamp(0-120*0.017,-15,15) = -2.04
-> converted to 357.96
frame 3: clamp(357.96 - 2.04,-15,15) = 15
... ... ...
and it will go back to 0 as long as you're jumping

So yeah using your RotateTo variable is a good idea (:

And for the dt... Basically to understand how things work, look at how the values evolve when using it.
dt is the time between the previous and the current frame.
in a 60fps game,
dt = 1/fps = 1/60 = 0.01666666666
(dt is the "second per frame" so spf = 1/fps :D )
let's simplify to 0.017

If you add dt to a value every frame:

After exactly one second, which is 60 frames, the value will be equal to
60*1/60 = 1

in a 30 fps game, after one second, which is 30 frames, you'll get
30*1/30 = 1

So basically, in whatever fps you are, the succession of addition will all reach 1 after one second.

if you add 5*dt, the value will reach 5 after one second
so basically, if you
set X to self.X + 5*dt
you add 5px to X every one second, so you move your object at exactly 5px per second

It works the same with angle:
set angle to self.angle + 5*dt
will rotate at 5 degree per second.

that's the holy magic of dt in a nutshell =)Yann2012-09-08 17:42:51
Posts: 1,481
Reputation: 16,400

Return to How do I....?

Who is online

Users browsing this forum: Nuobz and 1 guest