Changing Sprite animation and movement speed

Hi,

I am looking for a clever way for setting up a walking sprite that slowly accelerates from standing to normal walking speed.
The normal moving speed is 250 and normal animation frame speed is 45.
I have added the following lines in the Window.Update procedure.

The increasing moving is working sort of okay with the next 2 lines but the FramesPerSecond not; it keeps the initial animation speed of the animation after loading the sprite screen with TSprite.CreateFrameSize.

if Sprite[NR].MoveSpeed < 250 then Sprite[NR].MoveSpeed := Sprite[NR].MoveSpeed + 5;
Sprite[NR].CurrentSprite.X := Sprite[NR].CurrentSprite.X + SecondsPassed * Sprite[NR].MoveSpeed;
(works okay)

if Sprite[NR].WalkRightSprite.FramesPerSecond < 45 then Sprite[NR].WalkRightSprite.FramesPerSecond := Sprite[NR].WalkRightSprite.FramesPerSecond + 5;
(not working).

Do you have an example how to get this right?
Thanks.

Hmmm… I’m not sure if I can tell what exactly is not working in your code but it doesn’t seem to solve the problem you need to solve. Try this approach:

const
  FullSpeed = 250;
  Acceleration = 5; // in px/s^2
begin
  { Deal with Acceleration }
  // Note the use of "SecondsPassed" here!
  Sprite[NR].MoveSpeed += Accelration * SecondsPassed;
  if Sprite[NR].MoveSpeed > FullSpeed then
    Sprite[NR].MoveSpeed := FullSpeed;

  { Move the sprite}
  Sprite[NR].CurrentSprite.X += SecondsPassed * Sprite[NR].MoveSpeed;
end;

About the “FramesPerSecond” part, I understand you’re trying to speed up the animation, right? See Castle Game Engine: CastleGLImages: Class TSprite - in short, that’s not a reliable way to speed up the animation.
I don’t see any way to change speed of TSprite animations neither trhough TSprite nor through TSpriteAnimation APIs.

I can think of workarounds, but maybe it’s a much better way to try using this WIP solution Sprite Sheet Editor with our own format castle-sprite-sheet by and3md · Pull Request #248 · castle-engine/castle-engine · GitHub - I didn’t get to it myself, but it looks really promising.

Thanks.
I have looked at the new sprite handling format but don’t know if it can handle variable animation speed.
It is rather new and I probably need examples to get to it; I was just learning the old format.
But can I get variable animation speed option by converting the sprite sheets to X3D format?

As for the task you’re doing, you can also use SmoothTowards utility for this.

As for animation speed:

The new sprite sheet approach supports controlling the speed of the animation, using MyScene.TimePlayingSpeed (on a single scene) or MyViewport.Items.Items.TimeScale (scales all animations on all scenes).

It is much better than the TSprite approach (which will be soon deprecated) on all accounts, IMO :slight_smile: I really recommend the new approach :slight_smile: Note that it is already available and 100% working ( Sprite sheets · castle-engine/castle-engine Wiki · GitHub ), and you can use CGE editor to arrange scenes with sprite sheets visually.

The big thing that PR Sprite Sheet Editor with our own format castle-sprite-sheet by and3md · Pull Request #248 · castle-engine/castle-engine · GitHub adds is ability to design sprite sheets (from separate images) completely in CGE editor (before merging it, you should write Starling XML manually). The base of the sprite sheet work is already present in CGE (master branch), you can use sprite sheets using the new approach.

But can I get variable animation speed option by converting the sprite sheets to X3D format?

It is the same answer. “Converting the sprite sheets to X3D format” is what happens under the hood if you load the sprite sheet to TCastleScene. Usually, you don’t need to care about, and can consider “X3D nodes” as “some internal thing that engine is using”.

Ok, you convinced me to use the new format :slight_smile: but for me it’s starting from scratch again, as it already took me a lot of time to get some things done with the TSprite class.
So I need some CGE running examples to learn how I can “port” my game code so far from TSprite class code to the new format.
I haven’t seen any examples yet or I have missed something.

Indeed there are not so many examples in CGE examples now. I used sprite sheet in examples/creature_behaviors/ but that is a test in 3D.

One big example is being created by Andrzej Kilijański now.

That being said, you should not really that many examples :slight_smile: Because:

I hope this gets you started :slight_smile:

Thanks.
Well, one thing at a time. I guess I will have to learn about rendering with Scenes en Viewports first instead of my current TDrawableImage.
I will start a new post “Learning to use new Sprite method”. Expect a lot of questions along my new journey of discovery and errors. Thanks for your support.
:wink: