View Full Version : rotating around the origin with a reversed y-axis
Jesse Aldridge
05-18-2006, 02:12 AM
Ok, so this is the equation to rotate a point around the origin:
x' = cos(theta)*x - sin(theta)*y
y' = sin(theta)*x + cos(theta)*y
So with a reversed y-axis ( 0 is top, screen_height is bottom ) it would be like this right (just make the y negative) ?
x' = cos(theta)*x - sin(theta)* (-y)
y' = sin(theta)*x + cos(theta)* (-y)
Seems right, but I'm getting some weird trigonometry bugs, so I just thought I'd make sure I'm doing this right.
bodgey
05-18-2006, 03:12 AM
x' = cos(theta)*x - sin(theta)* (-y)
y' = sin(theta)*x + cos(theta)* (-y)
I think this should work:
x' = cos(theta)*x - sin(theta)* (-y)
y' = -(sin(theta)*x + cos(theta)* (-y))
since you're flipping the y axis, you have to flip it back when you're done. Haven't tried it though.
Edit: after simplifying it becomes this:
x' = cos(-theta)*x - sin(-theta)*y
y' = sin(-theta)*x + cos(-theta)*y
which is the same as before except you're rotating in the opposite direction.
Bojan
Mike Wiering
05-18-2006, 03:32 AM
x' = cos(theta)*x - sin(theta)*y
y' = sin(theta)*x + cos(theta)*y
All you want to do is flip the result vertially, so that would be:
x' = cos(theta)*x - sin(theta)*y
y' = -(sin(theta)*x + cos(theta)*y)
ggambett
05-18-2006, 08:11 AM
The 2D rotation equations don't assume anything about the coordinate axes except that they are perpendicular; flipping them or rearranging them will cause at most a clockwise-counterclockwise change, but the coordinates of the rotated point will always be numerically the same in the coordinate system you chose. What "trigonometric bugs" are you experiencing exactly?
Jesse Aldridge
05-18-2006, 02:25 PM
Ok, cool, so that answers my question.
I'm still getting the weird bugs though.
Here's my problem:
I do rotation here:
//Get opposing vector
Point attackVect = attacker.getStrikeVector();
Point deflectVect = new Point( attackVect );
//Reverse * 5
deflectVect = deflectVect.times( -5 );
deflectVect.rotate( Random.randRange(-10, 10) );
attacker.handleCountered( deflectVect );
and here
// Select a random point starting from the target's center, where the angle is
// the angle from the target to the attacker
double angleToA = Angles.getAngle( target.getCenter(), attacker.getCenter() );
Point a = Point.getPointAtAngle( target.getCenter(), new Angle( angleToA ),
target.getRadius() );
Point b = Point.getPointAtAngle( target.getCenter(), new Angle( angleToA + 180 ),
target.getRadius() );
attacker.setStrike( a, b, attackVel );
The first part only works if my rotation function looks like this:
result.x = Math.cos(angle) * p.x - Math.sin(angle) * p.y;
result.y = Math.sin(angle) * p.x + Math.cos(angle) * p.y;
The second part only works if my rotation function looks like this (negating the y component):
result.x = Math.cos(angle) * p.x - Math.sin(angle) * p.y;
result.y = -( Math.sin(angle) * p.x + Math.cos(angle) * p.y );
Here's some of the stuff that gets called in the above code:
class Point
{
//Set point to a normal vector pointing from p1 to p2
public Point( Point p1, Point p2)
{
if( p1 == p2 )
{
x = y = 0;
return;
}
double mag = GeometryUtils.dist( p1, p2 );
x = (p2.x - p1.x) / mag;
y = (p2.y - p1.y) / mag;
}
public void rotate( double angle )
{
set( Angles.rotatePoint( angle, this ) );
}
public void set( Point otherPoint)
{
x = otherPoint.x; y = otherPoint.y;
bSet = true;
}
public static Point getPointAtAngle(Point base, Angle angle, double d)
{
Point p = new Point(d, 0);
p.rotate( angle.getVal() );
p.x += base.x;
p.y += base.y;
return p;
}
}
class Angles
{
//Simply wraps a double
//in order to avoid confusion when passing both an angle
//and a double to a function
static public class Angle
{
public Angle(double val){ myVal = val; }
public double getVal(){ return myVal; }
public double val(){ return myVal; }
double myVal;
}
...
//Rotates around origin
public static Point rotatePoint(double angle, Point p)
{
angle *= Math.PI / 180;
Point result = new Point();
//x' = cos(theta)*x - sin(theta)*y
//y' = sin(theta)*x + cos(theta)*y
result.x = Math.cos(angle) * p.x - Math.sin(angle) * p.y;
result.y = -( Math.sin(angle) * p.x + Math.cos(angle) * p.y );
return result;
}
public static double getAngle(double x, double y, double x2, double y2)
{
double relX = x2 - x,
relY = y2 - y;
double baseAng = 0;
if(relX < 0)
baseAng = 180;
if(relX == 0)
if(relY > 0)
return 270;
else
return 90;
double angle = baseAng - Math.atan(relY/relX) * ( 180 / Math.PI );
return wrapAngle(angle);
}
public static double wrapAngle(double angle)
{
while(angle >= 360)
angle -= 360;
while(angle < 0)
angle += 360;
return angle;
}
}
class Fighter
{
Point getStrikeVector()
{
Point normalVect = new Point( strikePoint1, strikePoint2 );
return normalVect;
}
void handleCountered(Point enemyVector, Fighter enemy)
{
// Get my vector
Point newVelocity = getStrikeVector();
// Add enemy vector to the new velocity
newVelocity = newVelocity.plus( enemyVector );
bCountered = true;
counterVelocity = newVel;
}
}
vBulletin v3.6.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.