Commodore 64 (C64) Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Trying to add velocity to my sprites. (FIXED)

 
Post new topic   Reply to topic    Commodore 64 (C64) Forum Index -> Scene
View previous topic :: View next topic  
Author Message
cbmeeks
Master of C64
Master of C64


Joined: 22 Oct 2004
Age: 39
Posts: 1070
Location: Hixson, TN USA

PostPosted: Sun Mar 04, 2012 1:34 am    Post subject: Trying to add velocity to my sprites. (FIXED) Reply with quote

OK, so I have six sprites that are now all at position #30 and I am trying to move them in a straight line towards the right. Each at different velocities.

So here is what I have:

Code:

   ldx #0
   ldy #0
!loop:
   clc
   lda xpos_vel, y
   adc $d004, x
   sta $d004, x
   inx
   inx
   iny
   cpy #6
   bne !loop-

...

.pc = xpos_vel
   .byte 1, 2, 4, 2, 2, 1



I am reserving sprite 1 and 2 for my main character. But the above routine doesn't seem to work. What am I doing wrong?

** EDIT **

DUH!!! I forgot to add: sta $d004, x

LOL!! Nevermind. But maybe this might help someone else?


Now, when I set the Y positions, this is what I use and it works.

Code:

   ldx #0
   ldy #0
!loop:            // set y positions
   lda ypos, y
   sta $d005, x
   inx
   inx
   iny
   cpy #6
   bne !loop-
...

.pc = ypos
   .byte 48, 72, 96, 120, 144, 168


Thanks!
Back to top
View user's profile Send private message Visit poster's website
Rybags
Groupie in Training
Groupie in Training


Joined: 17 Mar 2006
Posts: 103

PostPosted: Sun Mar 04, 2012 6:03 am    Post subject: Reply with quote

Using straight integers for velocity values limits your options somewhat.

Only gives you 10 or so useful unique values per direction - anything bigger just becomes too fast to be very useful.

You can fairly easily employ "fractional" values by using 2 bytes per sprite axis instead of 1.

Have the integer "velocity/direction" value in one byte, "fractional velocity" in the other and have the fractional part get added to an "accumulator" value, and if it overflows, it gets added to the position on that occasion.

Use the high bit of the velocity/direction value to determine if the sprite moves in a positive or negative direction.

The coding is somewhat more complex than just doing movement by integer only but you end up with exponentially more useful velocity possibilities.

Best method is probably to add the fractional to the accumulator values, store it.
Then get the velocity/direction byte. PHP to save the "N" flag.
AND with #$7F to leave velocity only, ADC #0 to add the accumulator overflow that would be left in the Carry.
Then PLP to get the previous "N" state back. Accumulator has the velocity in force for the current iteration. Then you BMI to the code that subtracts velocity from the XPos, otherwise it gets added to the XPos.


The other advantages of fractional movement is the ability to do nice smooth acceleration, and very slow movement - down to about 1 pixel every 5 seconds.
Back to top
View user's profile Send private message
cbmeeks
Master of C64
Master of C64


Joined: 22 Oct 2004
Age: 39
Posts: 1070
Location: Hixson, TN USA

PostPosted: Mon Mar 05, 2012 6:48 pm    Post subject: Reply with quote

Wow. That's some insanely good ideas!!

Never thought about fractional movements. And I like the idea of using the high bit for positive/negative movement.

Thanks!!
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Commodore 64 (C64) Forum Index -> Scene All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Tip: Get C64 Forever for super-comfy C64 emulation with pre-installed games, demos and other goodies!


Powered by phpBB © 2001, 2005 phpBB Group