Page 1 of 1
Vectors
Posted: Sat Apr 07, 2007 6:55 am
by lizardkid
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.
Posted: Sat Apr 07, 2007 9:10 am
by jv_map
Posted: Sun Apr 08, 2007 12:38 am
by PKM
excellent system, if you could remake the original space duel with the connect ships option, even better.

Posted: Tue Apr 17, 2007 6:44 am
by lizardkid
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..
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.
Posted: Tue Apr 17, 2007 7:07 am
by jv_map
You don't have to convert
anything to degrees.. that's 1 problem, the Math.toDegrees is certainly incorrect here. Another (obvious

) thing is the -4.6, rather just add pi/2 or fix the atan2 line so it gives you the right angle right away

Posted: Tue Apr 17, 2007 9:37 am
by lizardkid
Well once i knew atan returns degrees it wasn't that bad. it works fine now; and i feel considerably stupider

Posted: Tue Apr 17, 2007 7:19 pm
by jv_map
Errm atan2 returns radians m8

. No need to feel stupid anyhow, I'm sure we've all been puzzled by atans at times

(ok or some might not even have tried (yet!

))
Posted: Tue Apr 17, 2007 10:07 pm
by lizardkid
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.