Author Topic: Moving sprites in a circle  (Read 11619 times)

0 Members and 1 Guest are viewing this topic.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Moving sprites in a circle
« Reply #15 on: April 06, 2010, 06:25:45 pm »
You can optimize that further to this:

Code: [Select]
Pt-On(RCos(Z),Rsin(Z
Where Z is the angle to rotate, starting from the X axis, and R is the distance from the origin.  This uses less trig, but then you have to do a  bit more math to start from a specific location.

I will be going over a lot more in my upcoming Trig tutorial, it should be up by the end of the day, i had a lot of free time in CompSci today ^^

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Moving sprites in a circle
« Reply #16 on: April 06, 2010, 06:32:49 pm »
so for example something like this? (TI-BASIC)

9->X
9->Y
ClrDraw
While 1
For(Z,0,359
Pt-On(X*cos(Z)-y*sin(Z),X*sin(Z)-y*cos(Z
End
End

right?

(Altough this example appears to be kinda buggy... the first time the dot is displayed at the highest top value it can be as well as the farthest to the right x.x)

Well your code only really seems to be buggy in the sense that "Y" gets erased do the the glitch. (So you were constantly adding or subtracting zero.)

As for the "+" and "-" it doesn't really matter which one is where, as long as you have both it will work. It will just start in a different quadrant.

This is what I kinda changed your code to (this uses what SirCmpwn said):
Code: [Select]
ZSquare
ClrDraw
For(Z,0,359
Pt-On(5cos(Z)+5sin(Z),5sin(Z)-5cos(Z
End

But like Builderboy said you can simplify the code to:
Code: [Select]
ZSquare
ClrDraw
For(Z,0,359
Pt-On(5cos(Z),5sin(Z
End
« Last Edit: April 06, 2010, 06:36:10 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

_player1537

  • Guest
Re: Moving sprites in a circle
« Reply #17 on: April 06, 2010, 07:45:14 pm »
I tried making a version of this in Axe, didn't really turn out too well, just looked like an hourglass.  Here's my code:
(note:I know this isn't optimized, I just wanted to get it to work at all, then optimizations)

Code: [Select]
[8000000000000000->Pic1
9->x->Y
0->Z
repeat getkey(15)
Z+90Sub(S)->D
ZSub(S)->E
Pt-On(5*D+30,5*E+30,Pic1
Z+1->Z
DispGraph
End
Return
Lbl S
->A^64/16->B
A^16->A
If B^2
16-A->A
End
A-(A*A*A/600->A
If B/2^2
-A->A
End
A
Return

SirCmpwn

  • Guest
Re: Moving sprites in a circle
« Reply #18 on: April 06, 2010, 07:46:35 pm »
Is routine S supposed to return the sine of Ans?

_player1537

  • Guest
Re: Moving sprites in a circle
« Reply #19 on: April 06, 2010, 07:47:10 pm »
yes, I'm using builderboy's sin routine

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Moving sprites in a circle
« Reply #20 on: April 06, 2010, 07:52:29 pm »
Oh its because the input is not in degrees.  Add 16, not 90, as a full rotation is 64, not 360.

_player1537

  • Guest
Re: Moving sprites in a circle
« Reply #21 on: April 06, 2010, 07:54:33 pm »
now it just displays a plus sign?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Moving sprites in a circle
« Reply #22 on: April 06, 2010, 08:08:10 pm »
Actually, cos(Z) = sin(90-Z), not sin(Z+90)

Edit:
Er, I meant 16 in this case :P
« Last Edit: April 06, 2010, 08:08:40 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

_player1537

  • Guest
Re: Moving sprites in a circle
« Reply #23 on: April 06, 2010, 08:16:26 pm »
It still shows a plus for some reason...  Here's my code again:
Code: [Select]
[8000000000000000->Pic1
9->x->Y
0->Z
repeat getkey(15)
16-ZSub(S)->D
ZSub(S)->E
Pt-On(5*D+30,5*E+30,Pic1
Z+1->Z
DispGraph
End
Return
Lbl S
->A^64/16->B
A^16->A
If B^2
16-A->A
End
A-(A*A*A/600->A
If B/2^2
-A->A
End
A
Return

Edit: Calc84 found the bug
Code: [Select]
A-(A*A*A/600->Ashould be
Code: [Select]
A-(A*A*A/600)->Athe '->' doesn't close parenthesis
« Last Edit: April 06, 2010, 08:19:49 pm by _player1537 »

Offline Will_W

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +1/-1
    • View Profile
Re: Moving sprites in a circle
« Reply #24 on: April 06, 2010, 08:20:05 pm »
You can approximate sin(x) with the parabola: y = 4/pi x - 4/pi^2 x^2 over the range [0,pi]
I have an implementation in z80 in my math.inc file.  Not sure how axe does sin and cos.
ex de,hl
ld (hl),e
inc hl
ld (hl),d
ld a,(hl)

_player1537

  • Guest
Re: Moving sprites in a circle
« Reply #25 on: April 06, 2010, 08:21:43 pm »
Axe doesn't have a built in sin routine yet.  I'll have to try what you were saying.
*looks for Will_W's math.inc file*

Offline Will_W

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +1/-1
    • View Profile
Re: Moving sprites in a circle
« Reply #26 on: April 06, 2010, 08:37:37 pm »
calc84 made some wicked optimizations to it, maybe he'll post them here if we're lucky.
Code: [Select]
Sin_A:
;input a in pi/256 radians from [0,pi)
;output l is a binary value in the range [0,1)
;destroys h,de
;a is unmodified
;Will West
ld h,a
ld e,h
call HtimesE;hl=x^2
ex de,hl;x^2

ld l,a
ld h,0
or a;reset carry
sbc hl,de
        add hl,hl
        add hl,hl
ret

HtimesE:
 ld l,0
 ld d,0
 
 sla h       ; optimised 1st iteration
 jr  nc,$+3
 ld  l,e

 add hl,hl       ;1
 jr  nc,$+3      ;
 add hl,de       ;
 
 add hl,hl       ;2
 jr  nc,$+3      ;
 add hl,de       ;

 add hl,hl       ;3
 jr  nc,$+3      ;
 add hl,de       ;
 
 add hl,hl       ;4
 jr  nc,$+3      ;
 add hl,de       ;
 
 add hl,hl       ;5
 jr  nc,$+3      ;
 add hl,de       ;
 
 add hl,hl       ;6
 jr  nc,$+3      ;
 add hl,de       ;
 
 add hl,hl       ;7
 jr  nc,$+3      ;
 add hl,de       ;
 
 ret

 
;;=================================================================================
;;does sin(a) where a is between (-pi,pi) and is in units of
;;128/pi radians
;;inputs a
;;outputs a
;;destroys bc,de,hl
;;=================================================================================
Sin_A_Signed:
 bit 7,a
 jr z,Sin_A_Signed2
 neg
 sla a
 call Sin_A
 ld a,l
 neg
 ret
Sin_A_Signed2:
 sla a
 call Sin_A
 ld a,l
 ret
« Last Edit: April 06, 2010, 09:30:39 pm by Will_W »
ex de,hl
ld (hl),e
inc hl
ld (hl),d
ld a,(hl)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Moving sprites in a circle
« Reply #27 on: April 06, 2010, 11:21:35 pm »
That looks almost right.
The Pt-On line should look like this:
         Look here↓
Pt-On(X*cos(Z)+Y*sin(Z),X*sin(Z)-Y*cos(Z
And, it can be optimized to this as well:
Pt-On(Xcos(Z)+Ysin(Z),Xsin(Z)-Ycos(Z   // Removed "*" tokens
Keep in mind that it rotates around the origin, or the 0, 0 point.  Also, you can keep adding 1 to Z, and it will still work.  Z does not have to be between 0 and 360, 361 will rotate it 1 degree.
woops lol why did I forgot about those *? :D, I guess when I am used more to Axe, if I decide to go back to BASIC afterward, I'll have to be careful ^^
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)