motorollin
Member
- Joined
- Jul 31, 2007
- Messages
- 163
Is it possible to specify an angle in degrees and a velocity for a sprite rather than X and Y velocities? Alternatively is it possible to make it move in a straight line towards a specified pixel?
skeezix said:Depends on the system you're using for handling sprites.
It's hand-coded using SDL - so my "sprite" is really just an image on a surface which gets pushed around the screen by changing its x and y velocities.
skeezix said:If you're coding it yourself you can do what you like.
Would you be able to point me towards an example? I have Googled but can't find what I'm looking for.
skeezix said:If you're using a sprite managemenbt library, it depends on that library.
Unless you're referring to SDL, I'm not.
Ok so what you're talking about is actually polar vs cartesian. A polar coordinate is a speed (scalar) and an angle (direction) and a cartesian is a vector with X and Y magnitudes that points at the target.motorollin said:Is it possible to specify an angle in degrees and a velocity for a sprite rather than X and Y velocities? Alternatively is it possible to make it move in a straight line towards a specified pixel?
If your playfield is a rectangle this is really easy, just negativize the appropriate element of the vector, if you hit a vertical wall change the y = -y or x = -x for a horizontal.motorollin said:My sprite starts at a random position in the playfield and needs to move off at a random angle. When it hits one of the edges of the playfield it deflects back in to the playfield. Short of telling the sprite to move in a direction then when it gets to the edge changing the angle to 180-angle, I don't know how else to achieve this.
So you mean somehting like this?motorollin said:Calculating the angle of deflection is not the problem I am having. It's what code to use to actually make my sprite move in a specified direction instead of doing "x+=5;y+=5;".
If calc_velocity() takes an angle in degrees as an argument, and then returns the necessary x and y velocities to make the sprite move in that angle, then yes. What that calc_velocity() function contains is what I can't get my head round :rolleyes:charlieb said:So you mean somehting like this?
CODE
velocity = calc_velocity();
sprite.x += velocity.x;
sprite.y += velocity.y;
draw(sprite);
Oh ok well that is really just basic trig:motorollin said:If calc_velocity() takes an angle in degrees as an argument, and then returns the necessary x and y velocities to make the sprite move in that angle, then yes. What that calc_velocity() function contains is what I can't get my head round :rolleyes:
motorollin said:That works! Thanks
Now, to work out *why* it works, so I understand for next time...
Thanks again!
Tough achieving a perfect angle on a raster display or even fudge something near it, for example 15 degrees closest fit may be over 5 and up 2 pixels which would have to be corrected on a supercell basis every so often to keep it anywhere near the neighborhood of 15 degrees. That might be what is in your function.motorollin said:Is it possible to specify an angle in degrees and a velocity for a sprite rather than X and Y velocities? Alternatively is it possible to make it move in a straight line towards a specified pixel?
This is why you should keep a floating/fixed point representation of the coordinates internally and round before plotting the sprite. You could re-calculate the vector every iteration but that would be more expensive (I would think)Sphinxter said:Tough achieving a perfect angle on a raster display or even fudge something near it, for example 15 degrees closest fit may be over 5 and up 2 pixels which would have to be corrected on a supercell basis every so often to keep it anywhere near the neighborhood of 15 degrees. That might be what is in your function.
charlieb said:This is why you should keep a floating/fixed point representation of the coordinates internally and round before plotting the sprite. You could re-calculate the vector every iteration but that would be more expensive (I would think)Sphinxter said:Tough achieving a perfect angle on a raster display or even fudge something near it, for example 15 degrees closest fit may be over 5 and up 2 pixels which would have to be corrected on a supercell basis every so often to keep it anywhere near the neighborhood of 15 degrees. That might be what is in your function.
charlieb
That would be irrational.