# Rotation...?

**843853**Jun 21, 2010 9:26 PM

I'm making a game in java, basically like a Tower Defense game.

I've been working on this for 4 days now and its going pretty nicely.

(I'm 15 and don't know math that well, so please don't yell at me about this 'probably' simple thing, lol)

I'm currently stuck with rotation, the rotation works but theres a issue...

The issue is that once the angle reaches 180 degrees and try's to increase more it'll instead go to -180.

I've been searching the internet for hourrrrrrss (a good 3 hours +)

Heres my code for rotation and setting rotation on a object:

I've been working on this for 4 days now and its going pretty nicely.

(I'm 15 and don't know math that well, so please don't yell at me about this 'probably' simple thing, lol)

I'm currently stuck with rotation, the rotation works but theres a issue...

The issue is that once the angle reaches 180 degrees and try's to increase more it'll instead go to -180.

I've been searching the internet for hourrrrrrss (a good 3 hours +)

Heres my code for rotation and setting rotation on a object:

```
//Setting of the rotations is only called when a new direction is required:
level.creep
```*.setRequiredRotation(getDirectionDegree(level.creep[i].getMoveY()[currentTileMove + 1], level.creep[i].getY(), level.creep[i].getMoveX()[currentTileMove + 1], level.creep[i].getX()));*

//Rotate the current rotation until we are at the required angle.

if(level.creep[i].getRotation() - level.creep[i].getRequiredRotation() > 10) {

level.creep[i].setRotation(level.creep[i].getRotation() - 10);

} else if(level.creep[i].getRotation() - level.creep[i].getRequiredRotation() < -10) {

level.creep[i].setRotation(level.creep[i].getRotation() + 10);

} else {

level.creep[i].setRotation(level.creep[i].getRequiredRotation());

}

public double getDirectionDegree(int y1, int y2, int x1, int x2) {

double direction = Math.toDegrees(Math.atan2(y1 - y2, x1 - x2));

return direction;

}
`Heres a cleaner code that is used by towers:`

for(int i2 = 0; i2 < level.creep.length; i2++) {

if(level.creep[i2] != null) {

Rectangle towerRangeRectangle = new Rectangle((level.tower[i].getX() / Tile.TILE_SIZE - level.tower[i].getRange() / 2) * Tile.TILE_SIZE, (level.tower[i].getY() / Tile.TILE_SIZE - level.tower[i].getRange() / 2) * Tile.TILE_SIZE, level.tower[i].getRange() * Tile.TILE_SIZE, level.tower[i].getRange() * Tile.TILE_SIZE);

Rectangle creepRectangle = new Rectangle(level.creep[i2].getX(), level.creep[i2].getY(), Tile.TILE_SIZE, Tile.TILE_SIZE);

if(towerRangeRectangle.intersects(creepRectangle)) {

target = i2;

level.tower[i].setRequiredRotation(getDirectionDegree(level.creep[i2].getY(), level.tower[i].getY(), level.creep[i2].getX(), level.tower[i].getX()));

}

}

}

//Rotate the turret.

if(System.currentTimeMillis() - level.tower[i].lastTurretMoveTime >= 75) {

if(level.tower[i].getRotation() - level.tower[i].getRequiredRotation() > 10) {

level.tower[i].setRotation(level.tower[i].getRotation() - 10);

} else if(level.tower[i].getRotation() - level.tower[i].getRequiredRotation() < -10) {

level.tower[i].setRotation(level.tower[i].getRotation() + 10);

} else {

level.tower[i].setRotation(level.tower[i].getRequiredRotation());

}

level.tower[i].lastTurretMoveTime = System.currentTimeMillis();

}
```
Uses the same method to convert the radians to degrees as the top.
Does also the same basic thing as the top.
Help would be very appreciated. Thanks.
Edited by: steve4448 on Jun 21, 2010 7:22 PM
```

- 1879 Views
- Tags: none (add)