Page 1 of 1

How to animate a sprite?

Posted: December 26th, 2013, 10:38 am
by jman
Hello,

I would like to learn how to manage sprite movement more efficiently.
There are a couple of things that I'm solving using a "brute force" approach and I wonder if there's a more "PSX-oriented" way of doing this.

I'll try to keep it short :-)

Basically what I would like to do is the following:
1_ Automatically animate a sprite according to the number of frames available
2_ Correct timing of the frames animation
3_ Tips in order to minimize calculations

(1) In order to "animate" a sprite I have to change GsSPRITE.u GsSPRITE.v coordinates relative to the TIM file. Currently I have a struct to manage an item on screen:

Code: Select all

struct MyHero
{
	GsIMAGE img;
	GsSPRITE sprt;
	...
};
Let's say I have a TIM file with four frames for the same sprite (see attachment).
I want to automatically invoke a animateSprite(GsSPRITE sprite) method that calculates the next frame to use.
I could add a "frame_index" property in the struct and keep track of the current frame; then proceed like this to calculate the offset of the TIM file:

Code: Select all

(TIM width / num_frames) * frame_index++
Is there something better?

(2) How do I correctly synchronize the sprite animation in order to prevent a visual flickering?

(3) What is an efficient way to organize sprite frames in the TIM file in order to minimize these calculations?
Examples: the first row/column of the TIM file frame for basic sprite movement, the second one is for explosion animation, and so on... suggestions?

Thanks

Re: How to animate a sprite?

Posted: December 26th, 2013, 12:27 pm
by inc^lightforce
You can have my Source Code that i wrote in 2010.
Something isn't used, but all necessary files are included in the package i think :)

see Example:
[youtube]http://www.youtube.com/watch?v=MGpOiG5goxM[/youtube]

the jumping Mouse is the animated Sprite called "mumie" in the Source Code

PACK here:
http://rapidshare.com/share/7F7A51B93E6 ... 45535A56A5

shouts
inc.

Re: How to animate a sprite?

Posted: December 27th, 2013, 9:16 am
by jman
inc^lightforce wrote:You can have my Source Code that i wrote in 2010.
Did everyone in the scene used to write code like that? :-)
Anyway, thank you very much, I think I got the idea: I can only prepare an array of GsSPRITE and then manually do some calculations to do what I really want (see my previous post). I'll work a bit more on this.

For anyone interested I attach source code, TIM file and resulting BIN of a sample using Orion_'s wrapper.
The Sonic sprite is taken from a post of user "bizarro2011", the frames are not perfectly aligned so there's a slight movement effect.

EDIT: uploaded and replaced attachment with a revised version of the code

Re: How to animate a sprite?

Posted: December 27th, 2013, 10:53 am
by inc^lightforce

Re: How to animate a sprite?

Posted: December 27th, 2013, 11:30 pm
by jman
I love demos and related productions, there really is some art in it. I could spend hours staring at them.
However taken as tutorial code, they are sometimes a bit hard to follow. If I wrote code like that at work I would be fired! :-D

Re: How to animate a sprite?

Posted: December 28th, 2013, 1:51 am
by inc^lightforce
If I wrote code like that at work I would be fired! :-D
ha ha :D
we call it "leisure Coding"

feel free to check some of my other code-samples here: http://www.psxdev.net/forum/viewtopic.php?f=41&t=272

cheers