Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help moving a sprite in a ellipse
#1
Can somebody help me figure out the code to get a sprite to follow an ellipse? I've tried various methods with SIN() but can't quite pull it off, I am trying to recreate the effect in the attached gif


[Image: Bounder-Tower.gif]
Reply
#2
Here is the simplest example I could come up with. This code creates 360 points around a circle. By varying XSIZE and YSIZE you can squash (YSIZE) or elongate (XSIZE) the ellipse.

Replace the CIRCLE command with your sprite's _PUTIMAGE statement.

Code: (Select All)


CONST RADIUS = 200

CONST XSIZE = 1
CONST YSIZE = .5


DIM x(359) AS SINGLE
DIM y(359) AS SINGLE

FOR i = 0 TO 359
    x(i) = SIN(_D2R(i)) * RADIUS * XSIZE
    y(i) = -COS(_D2R(i)) * RADIUS * YSIZE
NEXT i

SCREEN _NEWIMAGE(640, 480, 32)
CLS

i = 0
DO

    _LIMIT 60
    CLS
    CIRCLE (319 + x(i), 239 + y(i)), 20

    i = i + 1
    IF i = 360 THEN i = 0
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#3
Here is one sprite in constant ellipse Y- and b+ constantly tilting ellipse by a degree after every orbit:
Code: (Select All)

_Title "Experiment with Ellipse draw sprite" 'b+ 2023-07-05
Type xy
    As Single x, y
End Type

Screen _NewImage(800, 600, 32)
_ScreenMove 250, 60
ReDim ell(0) As xy, ell2(0) As xy
MapEllipse 400, 300, 250, 100, ell() '  x,y track for b+
MapEllipse 400, 300, 100, 250, ell2() ' x,y track for Y-
Sprite = _NewImage(40, 40, 32)
_Dest Sprite
Line (0, 0)-(39, 39), &HFF0000FF, BF
_PrintMode _KeepBackground , Sprite
_PrintString (12, 12), "b+"
Sprite2 = _NewImage(40, 40, 32)
_Dest Sprite2
Line (0, 0)-(39, 39), &HFFFFFF00, BF
_PrintMode _KeepBackground , Sprite2
Color &HFF000000
_PrintString (12, 12), "Y-"

_Dest 0
Dim temp As xy
While _KeyDown(27) = 0
    Line (0, 0)-(_Width, _Height), &H05000000, BF
    frame = frame + 1
    If frame = 360 Then frame = 0: tilt = tilt + 1
    Rotate 400, 300, ell(frame).x, ell(frame).y, tilt, temp
    _PutImage (temp.x, temp.y)-Step(_Width(Sprite), _Height(Sprite)), Sprite, 0
    _PutImage (ell2(frame).x, ell2(frame).y)-Step(_Width(Sprite), _Height(Sprite)), Sprite2, 0
    _Display
    _Limit 120
Wend

Sub MapEllipse (xc, yc, xRadius, yRadius, a() As xy)
    ReDim a(360) As xy
    For a = 0 To _Pi(2) Step _Pi(2 / 360)
        a(i).x = xc + xRadius * Cos(a)
        a(i).y = yc + yRadius * Sin(a)
        i = i + 1
    Next
End Sub

Sub Rotate (cx, cy, x, y, degAng, a As xy)
    'cx, cy is center of rotation
    'x, y the point to rotate
    'degAng the angle in degrees to rotate
    ' output a.X, a.Y ' our point of rotation
    d = _Hypot(cx - x, cy - y) ' distance between xc and x
    ang = _Atan2(y - cy, x - cx)
    a.x = cx + d * Cos(ang + _D2R(degAng))
    a.y = cy + d * Sin(ang + _D2R(degAng))
End Sub
b = b + ...
Reply
#4
(07-06-2023, 01:48 AM)bplus Wrote: Here is one sprite in constant ellipse Y- and b+ constantly tilting ellipse by a degree after every orbit:

Oh, I did not know that _PI carries a multiplier parameter and that you can _PI(2) instead of _PI * 2

It's good to have one's _PI horizons expanded. Wink
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#5
Yeah _Pi() is a handy multiplier function Smile

Would it be anymore efficient if QB64pe had a multiplier for RND?

I should note my ellipse demo was a quick throw up and first best mod would be to use the ell() as sprite centers as apposed to Left, Top corners ie subtract -_Width(sprite) *.5, _height(sprite) and Step (_width(_sprite, _height(sprite))... in the destination section of _putimage....  that would center the 2 sprites to the center of screen.

Compare Terries always recalcs the sprite positions, mine calcs all positions before running once and then for first sprite to do tilts useing the rotate sub to adjust for tilts in orbits.
b = b + ...
Reply
#6
(07-06-2023, 12:13 PM)bplus Wrote: Compare Terries always recalcs the sprite positions, mine calcs all positions before running once and then for first sprite to do tilts useing the rotate sub to adjust for tilts in orbits.

Huh? The example I posted performs all the calculations beforehand and stores the results in X() and y().
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#7
Oops! sorry Terry, once again I didn't read carefully, I assumed. Really embarrassing.  Angry

Between time Cobalt posted and I got my demo ready, Terry had posted, I mostly just ran his program to see what it did.
b = b + ...
Reply
#8
Awesome, thanks guys.

So I was forgetting COS, well I least I had half of what I needed to start with.

now just 56 more monster groups to get programed!

I'll just spend a few hours finishing up a few of them and then upload an updated demo.
Reply
#9
(07-06-2023, 01:02 PM)bplus Wrote: Oops! sorry Terry, once again I didn't read carefully, I assumed. Really embarrassing.  Angry

Between time Cobalt posted and I got my demo ready, Terry had posted, I mostly just ran his program to see what it did.

No worries Smile
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply




Users browsing this thread: 1 Guest(s)