#9978 - johnny_north - Sat Aug 23, 2003 9:23 pm
I believe that I understand what you're getting at, but I might take the sin/cos approach. I think that this will work if you want the rotated sprite to move in the direction it is pointing. This might be somewhat basic and more experienced folks could elaborate and make corrections if I'm wrong.
If you know the angle of rotation of your sprite, you can apply sin/cos to get the new x and y cords of a move. The first thing you might do is set up a look up table for sin/cos values in terms of degrees using fixed point math. Dovoto's Pern Project has the code to do this, but I recommend putting it all in a .h file so you don't use iwram, but that's up to you.
Once you're this far, you can take the angle of your rotated sprite and use it to get the new x,y coords of your sprite given it speed...
spriteAngle = 0; //angle interms of degrees
RotateSprite(spriteAngle);
spriteX += COS[spriteAngle] * spriteSpeed;
spriteY += SIN[spriteAngle] * spriteSpeed;
The thing to keep in mind is that the COS/SIN arrays are going to return values that are less than or equal to 1 for all 0-359 degrees so you'll have to be aware of this. I think to compenstate for this, you might make all of these variables based on fixed numbers. Also, take into condiseration where on the sprite's bitmap is the center and the starting angle of your sprite. I.E. if your starting unrotated sprite is pointing up and you associate this with 0 degrees of rotation, you'll get movement in the wrong direction.
angle = 0;
(COS[angle], SIN[angle]) = (1, 0) and that's movement directly to the right, not up or (0,-1). If you're new to this, planning ahead like this will save a little time.
If you need to set up a tracking algoithm where you know the position of your sprite and you know the position of P and you need to calculate the angle to rotate the sprite towards P, you can use the same math I think.
#14134 - plasma - Wed Dec 31, 2003 3:01 am
i tried moving a bullet based on the angle of the player using your algorithm, but work it does not. the bullet's sprite blinks rapidly and somehow it changes size & shape
_________________
ΩΠΣ
#14153 - sajiimori - Wed Dec 31, 2003 5:44 am
Sounds like you're moving your sprites much farther than you intended. The sizes and shapes change because the x and y values are overflowing into the other sprite attributes.
Check your math. The algorithm is correct as posted.
#14162 - plasma - Wed Dec 31, 2003 7:11 am
Actually, that causes numerous compile errors
_________________
ΩΠΣ
#14318 - plasma - Sun Jan 04, 2004 6:45 am
Sajiimori's right - the algorithm is correct. Sure, I had to divide it by 20 to make it visible, but it works