PDA

View Full Version : Rotation Warping


StealthyCoin
2006.11.27, 07:08 PM
So im trying to rotate a primative shape. Here is my movment update code, so you can see what the keys are really doing.

public void checkInput(Sprite player, long elapsedTime) {
if (keys.w()) {
player.setVelocityY(-PlayerSprite.SPEED);
}
else {
player.setVelocityY(0);
}
if (keys.s()) {
player.setVelocityY(PlayerSprite.SPEED);
}
if (keys.d()) {
player.setVelocityX(PlayerSprite.SPEED);
}
else {
player.setVelocityX(0);
}
if (keys.a()) {
player.setVelocityX(-PlayerSprite.SPEED);
}

//rotation
if (keys.left()) {
player.setAngle(player.getAngle() - PlayerSprite.ROTATION * elapsedTime);
}
if (keys.right()) {
player.setAngle(player.getAngle() + PlayerSprite.ROTATION * elapsedTime);
}
if (keys.up()) {
player.setVelocityX((float)Math.cos(Math.toRadians (player.getAngle() - 90)));
player.setVelocityY((float)Math.sin(Math.toRadians (player.getAngle() - 90)));

Vector velocity = new Vector(player.getVelocityX(), player.getVelocityY());
velocity = Vector.normalize(velocity);

player.setVelocityX(velocity.x * PlayerSprite.SPEED);
player.setVelocityY(velocity.y * PlayerSprite.SPEED);
}
if (keys.down()) {
player.setVelocityX((float)-Math.cos(Math.toRadians(player.getAngle() - 90)));
player.setVelocityY((float)-Math.sin(Math.toRadians(player.getAngle() - 90)));

Vector velocity = new Vector(player.getVelocityX(), player.getVelocityY());
velocity = Vector.normalize(velocity);

player.setVelocityX(velocity.x * PlayerSprite.SPEED);
player.setVelocityY(velocity.y * PlayerSprite.SPEED);
}
}


public boolean inBounds(Polygon p) {
int x[] = p.xpoints;
int y[] = p.ypoints;
int len = p.npoints;
for (int i = 0 ; i < len ; i++) {
Point point = new Point(x[i], y[i]);
if (point.x < 0 || point.y < 0) {
return false;
}
if (point.x > RunnerApp.WIDTH || point.y > RunnerApp.HEIGHT) {
return false;
}

}
return true;
}

public void updateSprite(Sprite sprite, long elaspedTime) {
float oldX = sprite.getX();
float newX = oldX + sprite.getVelocityX() * elaspedTime;
float oldY = sprite.getY();
float newY = oldY + sprite.getVelocityY() * elaspedTime;

PlayerSprite player = (PlayerSprite)sprite;

if (inBounds(player.getShape())) {
sprite.setX(newX);
sprite.setY(newY);
}
}

Here is the code for the acutal thing im trying to rotate.

/*public Point rotatePoint(Point p, Point origin, float deg) {
double xl = p.x-origin.x;
double yl = p.y-origin.y;

double a = Math.toRadians(deg);
double cosa = Math.cos(a);
double sina = Math.sin(a);

double x = origin.x + xl*cosa - yl*sina;
double y = origin.y + yl*cosa + xl*sina;
p.x = (int)x;
p.y = (int)y;
return p;
}*/

public Point rotatePoint(Point p, Point origin, float deg) {

//double x = origin.x + (Math.cos(Math.toRadians(deg)) * (p.x - origin.x) - Math.sin(Math.toRadians(deg)) * (p.y - origin.y));
// double y = origin.y - (Math.sin(Math.toRadians(deg)) * (p.x - origin.x) - Math.cos(Math.toRadians(deg)) * (p.y - origin.y));


//double x = origin.x + p.y * Math.cos(Math.toRadians(deg)) + p.y * Math.sin(Math.toRadians(deg));
//double y = origin.y + p.x * Math.sin(Math.toRadians(deg)) + p.y * Math.cos(Math.toRadians(deg));

double x = origin.x + (p.x - origin.x) * Math.cos(Math.toRadians(getAngle())) - (p.y - origin.y) * Math.sin(Math.toRadians(getAngle()));
double y = origin.y + (p.x - origin.x) * Math.sin(Math.toRadians(getAngle())) + (p.y - origin.y) * Math.cos(Math.toRadians(getAngle()));

p.x = (int)Math.round(x);
p.y = (int)Math.round(y);
return p;
}

Here is an example of what happens. coin.gryphonsoftware.net download rotate.jar. CLick in the screen and hit the left right keys as indicated by the code above to "rotate" it.

Ive already nuked the IRC room with this problem and im sorta out of resources for fixing it. Ill mail you a hug if you find the cause of it :love:

skyhawk
2006.11.29, 03:55 AM
2 broken links

StealthyCoin
2006.11.29, 04:16 PM
2 broken links


I had code tags but they were ridiculously small. I guess Ill put them back.