Another side project of mine, not related to MOH at all. It's a 2D platformer.
I'm trying to make a system where sprites move with vectors, in a (speed, direction) style of working. The direction would be angle in degrees (or radians, the sprites rotate in radians but degrees are much easier to think about).
Well, that's simple enough. My problem is, i can't find a way to convert a speed and direction into base x and y speeds. Ideally, i want a sprite to move in any direction at a set maximum speed, only by modifying the direction it's facing.
Vectors
Moderator: Moderators
having really annoying and inconsistant problems. It's definitely not the polar coords, those seem to work; the problem is the angles i think.
See, to rotate my images i store angle in radians. But in the Vector is keep it in degrees. Somehow i must be losing precision.
Here's what happens, My sprites take off in an arc towards their target. I have to continually update their velocity to make them go the right direction. They should, of course, be able to speed right up to the target. When they get close to the target, however, they start strafing side to side and backwards. They eventually hit it, but it takes forever.
I don't see a way around it other than posting code; but this is annoying..
As i said, i need radians to draw the image but degrees to use polars. I've done debug draws and the vectors' angles are most certainly in degrees, but end up in the negatives more often than not; and sometimes go far past the 360 mark (i saw as high as 600).
I hate posting big code sections and saying "fix it for me" but i've tried everything; the problem doesn't seem to be consistant enough to pin down.
See, to rotate my images i store angle in radians. But in the Vector is keep it in degrees. Somehow i must be losing precision.
Here's what happens, My sprites take off in an arc towards their target. I have to continually update their velocity to make them go the right direction. They should, of course, be able to speed right up to the target. When they get close to the target, however, they start strafing side to side and backwards. They eventually hit it, but it takes forever.
I don't see a way around it other than posting code; but this is annoying..
Code: Select all
public void moveTo(double x, double y, double maxSpeed)
{
// scale the speeds; this just makes sure they slow down as they
// get close to the target; works fine.
double speed = Common.distTrace(x, y, originX+(width/2), originY+(height/2))/125;
if(speed > maxSpeed)
speed = maxSpeed;
else
if(speed < .75)
speed = 0;
// Here's the relevant parts.
// \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/
rotateTo(x, y);
setVelocity(new Vector(speed, Math.toDegrees(angle)));
}
public void setVelocity(Velocity newVal)
{
// velocity is the sprites' Vector. v1 is speed, v2 is direction.
velocity = newVal;
double newXX, newXY;
double theta = velocity.v2;
newXX = velocity.v1*Math.cos(theta);
newXY = velocity.v1*Math.sin(theta);
// the Y is negative because of the drawing system.
// increases as it goes down.
setSpeed(newXX, -newXY);
}
public void rotateTo(double x, double y)
{
double newAngle;
double x1, y1;
// sprite X and Y are from top left, so this makes sure x1 and y1
// are the center of the sprite.
// x2 and y2 are already like that.
x1 = this.getX()+width/2;
y1 = this.getY()+height/2;
newAngle = Math.atan2(y1-y2, x1-x2);
this.setAngle((newAngle)-4.6);
// i have no idea why i need to subtract 4.6, the image doesn't
// display correctly unless i do. Guessing this is part of my problem.
}As i said, i need radians to draw the image but degrees to use polars. I've done debug draws and the vectors' angles are most certainly in degrees, but end up in the negatives more often than not; and sometimes go far past the 360 mark (i saw as high as 600).
I hate posting big code sections and saying "fix it for me" but i've tried everything; the problem doesn't seem to be consistant enough to pin down.
Moderator
۞
Abyssus pro sapientia
Olympus pro Ignarus
۞
AND STUFF™ © 2006
۞
Abyssus pro sapientia
Olympus pro Ignarus
۞
AND STUFF™ © 2006
i went to atan2 and used y1-y2, x1-x2; which returns radians as you said.
I still need the -4.6 for correct image rotation though; and i don't know how it's getting to degrees for the polar coords part.
But i'm not going to question it; works perfectly. If it works in all cases and you don't know why it does, i think it's fine to let it lie and come back to it later... after i know about atans.
I still need the -4.6 for correct image rotation though; and i don't know how it's getting to degrees for the polar coords part.
But i'm not going to question it; works perfectly. If it works in all cases and you don't know why it does, i think it's fine to let it lie and come back to it later... after i know about atans.
Moderator
۞
Abyssus pro sapientia
Olympus pro Ignarus
۞
AND STUFF™ © 2006
۞
Abyssus pro sapientia
Olympus pro Ignarus
۞
AND STUFF™ © 2006


