This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenGoo
GreenGoo wrote ...

2013/3/27

Double to int

GreenGoo GreenGoo

2013/3/27

#
I need to turn a double variable into an int variable and make sure it rounds correctly. So far I have been unable to do this and my scenario has not worked properly as a result. Can anyone help me?
Duckyroannubbles Duckyroannubbles

2013/3/27

#
int b = (double) a;
Where a is your double. I am not 100% sure I have this right because I don't use it very often, but I'm like 90%.
davmac davmac

2013/3/27

#
Correct version of the above would be:
int b = (int) a;
However, this rounds towards 0. GreenGoo, what do you mean by "rounds correctly"?
Duckyroannubbles Duckyroannubbles

2013/3/27

#
Quite right, sorry.
GreenGoo GreenGoo

2013/3/28

#
Here is my code: double number = Math.atan((missileX - playerX)/(playerY - missileY)); int niceNumber = (int)Math.round(number); The double comes to 69.44395478, which should be rounded to 69. If it were 69, my code would work, but it does not, so I am assuming that my method of rounding creates a problem.
GreenGoo GreenGoo

2013/3/28

#
Your method : double number = Math.atan((missileX - playerX)/(playerY - missileY)); int niceNumber = (int) number;
GreenGoo GreenGoo

2013/3/28

#
Does not work either. My missile still goes round in a circle.
danpost danpost

2013/3/28

#
Try changing
double number = Math.atan((missileX - playerX)/(playerY - missileY));
// to
double number = Math.atan2(missileY - playerY, missileX - playerX);
GreenGoo GreenGoo

2013/3/28

#
It says 'method atan2 in class java.lang.Math cannot be applied to give types; required: double,double; found:int; reason:actual and formal arguement lists differ in length'
danpost danpost

2013/3/28

#
Yeah, it takes double values. Change line 3 to:
double number = Math.atan2((double)(missileY-playerY), (double)(missileX-playerX));
to turn number into degrees (instead of radians):
double number = Math.atan2((double)(missileY-playerY), (double)(missileX-playerX))*180.0/Math.PI;
davmac davmac

2013/3/28

#
to turn number into degrees (instead of radians):
Use Math.toDegrees()!! :)
GreenGoo GreenGoo

2013/3/28

#
Well, I have no compile errors, but I still have my original problem- my missile does not home in accurately. I have tried using both methods of rounding.
davmac davmac

2013/3/28

#
It seems to me that you just assumed rounding was the problem without any reason. If you're trying to get the missile to go towards the player, you should probably have:
double number = Math.toDegrees(Math.atan2((double)(playerY-missileY), (double)(playerX-missileX)));
I.e. subtract the missile coordinates from the target coordinates, not the other way around. However, even if that's wrong, it doesn't explain why your missile goes in a circle. Perhaps you need to post more of your code.
GreenGoo GreenGoo

2013/3/28

#
There are 8 different cases to program, depending on whether the x and y co-ordinates of both the missile and the player are equal, smaller, or greater than one another. This one only applies if the missile has a greater X but a smaller Y than the player. if (exploded == false && genericDelay < 0){ Actor player = (Actor) getWorld().getObjects(Player.class).get(0); int missileX = this.getX(); int missileY = this.getY(); int playerX = player.getX(); int playerY = player.getY(); double number = Math.atan(missileX - playerX / playerY - missileY); int niceNumber = (int)Math.round(number) ; if (missileX > playerX && missileY < playerY && this.getRotation() != (180 - (90 - 69))) { turn(3); } }
GreenGoo GreenGoo

2013/3/28

#
I have managed to solve the problem! The atan() was not happy about using integers instead of doubles. Then I made a host of other changes. Here is my current code: if (exploded == false && genericDelay < 0){ Actor player = (Actor) getWorld().getObjects(Player.class).get(0); double missileX = (double) this.getX(); double missileY = (double) this.getY(); double playerX = (double) player.getX(); double playerY = (double) player.getY(); double number = (missileX - playerX) / (playerY - missileY); double tanNumber = Math.toDegrees(Math.atan(number)); int niceNumber = (int)Math.round(tanNumber); if (missileX > playerX && missileY < playerY ) { int currentRotation = this.getRotation(); int diff = (90 + niceNumber) - currentRotation; int turn; if (diff > 0 )
You need to login to post a reply.