Page 1 of 1

Variable sprite speed

Posted: Tue Jun 19, 2018 10:19 pm
by r-tea
Hi!
I did my first try with Sprites and drawing.
It's a randomly moved circle.
There are two things that surprised me.
1. sprite often changes its speed significantly. The speed is constantly set to 1.
2. sprite's paths are not straight lines
Twice: why?

Code: Select all

@VERSION 2,0

@DISPLAY {Width = 640, Height = 480}
@SAMPLE 10, "Bells.wave"

/*
** Make the sprites. We create two sprites from each form: One for the
** background and one that is displayed below the mouse pointer.
*/
Function p_CreateSprites()
        ; Circle
		SetFillStyle(#FILLCOLOR)
        CreateBrush(10, 12, 12)
        SelectBrush(10)
        Circle(0, 0, 6, #WHITE)
        EndSelect
        SetBrushTransparency(10, #BLACK)
        CreateSprite(1, #BRUSH, 10)
EndFunction



p_CreateSprites()

x1=0
y1=0
DisplaySprite(1, x1, y1)

Function p_MoveRnd()
	x=Rnd(634)
	y=Rnd(474)
	Circle(x,y,3,#RED)
	MoveSprite(1, x1, y1, x, y, {Speed=1})
	PlaySample(10)
	Circle(x,y,3,#BLACK)
	x1=x
	y1=y
EndFunction

EscapeQuit(True)
SetInterval(1, p_MoveRnd, 40)

;
; main loop!
;
Repeat
        WaitEvent
Forever

Re: Variable sprite speed

Posted: Fri Jun 22, 2018 3:34 pm
by airsoftsoftwair
Don't use MoveSprite(). To move a sprite to a new position just call DisplaySprite() with the new position. MoveSprite() is a very old function of very limited practical use. And yes, the name is misleading but it got its name somewhen in 2001 or so ;-)

Re: Variable sprite speed

Posted: Sun Jun 24, 2018 11:26 pm
by r-tea
Ok, but DisplaySprite() just blanks the sprite on the old position, and display it instantly again in a new position, without displaying all the indirect positions. If MoveSprite() is not recomended then what alterative is there?

Re: Variable sprite speed

Posted: Mon Jun 25, 2018 12:12 pm
by SamuraiCrow
Use the law of cosines to find the direction of the difference of the coordinates and then convert the vector back to rectangular coordinates using sine and cosine. Just watch out for horizontal and vertical cases because they can result in an invalid angle so just do them manually using sign comparisons.

Re: Variable sprite speed

Posted: Mon Jun 25, 2018 5:39 pm
by r-tea
@SamuraiCrow
But it doesn't resolve the problem of speed.

EDIT:
Oh, you might have in mind DisplaySprite() to take evaluated coords. Am I right?
I was thinking you're refering to MoveSprite() at first.

Re: Variable sprite speed

Posted: Mon Jun 25, 2018 10:39 pm
by SamuraiCrow
r-tea wrote: Mon Jun 25, 2018 5:39 pm @SamuraiCrow
But it doesn't resolve the problem of speed.

EDIT:
Oh, you might have in mind DisplaySprite() to take evaluated coords. Am I right?
I was thinking you're refering to MoveSprite() at first.
DisplaySprite() is what I'm referring to. The speed is 1 by default when using trigonometric unit vectors. If you want it to be a different speed, multiply the unit vectors by the speed while they are still in polar notation.

Re: Variable sprite speed

Posted: Mon Jun 25, 2018 11:56 pm
by airsoftsoftwair
By the way, if you non-perfect vsync'ed refresh by "variable speed", then you have to use a plugin like GL Galore or RebelSDL which uses hardware double buffering. Hollywood's default display driver is software-based and thus doesn't offer perfect vsyncing which can lead to a few glitches when moving sprites. If you need perfect vsync'ing, you need to use GL Galore or RebelSDL because those two offer hardware double buffering. Then the speed will be completely smooth.

Re: Variable sprite speed

Posted: Tue Jun 26, 2018 8:54 pm
by r-tea
SamuraiCrow wrote: Mon Jun 25, 2018 10:39 pm DisplaySprite() is what I'm referring to. The speed is 1 by default when using trigonometric unit vectors. If you want it to be a different speed, multiply the unit vectors by the speed while they are still in polar notation.
As Hollywood's documentation stands DisplaySprite() doesn't have a speed argument.

Re: Variable sprite speed

Posted: Tue Jun 26, 2018 10:22 pm
by SamuraiCrow
I am telling you how to do the math to make it yourself. It takes some basic trigonometry.

Re: Variable sprite speed

Posted: Sat Dec 15, 2018 10:20 am
by Clyde
I would be interested in some kind of small tutorial or some explanations, if you still want to. Thanks!