Joehot200 - 2 years ago 91
Java Question

# How can I rotate the CannonBall around the ship?

Sorry about the bad title, but I could not think of anything else.

Let me explain: I have got 2 ships. The ship in the collision world, and the ship in the real world.

Now the ship in the real world is rotating, however I am unable to rotate the ship in the collision world (Or move it), which means that the collision objects must be rotated around the ship in the collision world.

When a CannonBall is near the ship, I get the distance from the CannonBall to the ship (for x, y, and z) and then compare that to a triple array of coordinates which are the collision points (About 2000+) - If the result from the triple array is "1", then they have collided. In other words, the collision ship does not move, but in the collision code the CannonBall's location is the same as the distance to the real-world ship and the real-world CannonBall.

Now here is the problem: If the ship is rotated, I need to rotate the CannonBall around the ship, because the ship cannot rotate.
How would I do this? What sort of mathematical formula?
Note: I have - Ship's rotation, x, y, and z coordinates, and the CannonBall's x, y, and z coordinates.

Thanks if anybody can give me an answer!

Edit: Lets make this more clear.
In the real world, ship1 could fire a CannonBall at ship2. If I refer to "The ship", I mean ship2.
When the CannonBall gets near to the ship, I want it to check for collisions. To do this,(Physics world) I check if

array[CannonBall.distance(ship).x][CannonBall.distance(ship).y][CannonBall.distance(ship).z]
returns a "1", and if it does, it collides.
However, because I cannot change an array without a large amount of processing, I cannot change the ship's rotation. Therefore, I must rotate the CannonBall around it so that it is relative to the CannonBall's true rotation compared to the ship.

If I got you right, then you are having a ship (e.g. in the middle of the screen) that has a canon ball rotating around it in order to aim towards another ship that is movable. All of that in 3D(?). Assuming that is what you mean, here is an answer ;) :

Lets do that in a 2 Dimensional Space first. What you need first is a formula to calculate a circle. As a function can only have one y-Value for each x-Value, you will actually need two. The smartest thing to do would probably divide the direction your canon ball is facing from the static ship into the common 360°, then have the formula calculate x and y coordinates. One approach could work with the formula for a circle (+/-r²=x²+y², with r being the radius), but I think a approach with cos and sin would be easier. Therefore: You might want to have an input of an angle. If this angle is 90°, then your cannon ball is to the right of the ship. 180° would be underneath your ship, and 270° to the left, 0°/360° would be above. I would then take that input and add 90° to it, so that you can start to the left of your ship (IMPORTANT for cos and sin to work!). What we know now is the angle of the cannon ball, and the radius of the circle. Assuming that you know the coordinates of the middle of the ship, you can then imagine yourself in a coordinate system, with your ship being the origin. Now you need to calculate x. This can be done by: x = -cos^-1(ANGLE)*radius. The result will be your x-Coordinate, that you have to subtract from the middle of the ship coordinate. Then, with x and the Angle being known, you can calculate y= x/tan-1(ANGLE). If the original angle is greater then 90°, you have to multiply the y coordinate with -1. The method for this would be:

public void calculateCordinates(int degrees, int r){
degrees = degrees+90;
while(degrees>=360){degrees=degrees-360;}//ensures that degree < 360°
double x;
double y;
}

The Coordinates are now in double as the results are most of the time not going to be whole numbers. Round them to ints before you subtract them from the origin coordinates.

If you want to do this in 3D, you can calculate the y coordinate by using the same method. This time, the angle you enter is your 3rd Dimension angle, so "the angle that you look at as in into the screen, out of the screen, with your right ear on the screen and so on" (sorry, didn't know how to describe that better). Of course you can combine the Methods to one as well, would look something like that:

public void calculateCordinates(int degrees, int degrees3D, int r){
degrees = degrees+90;
while(degrees>=360){degrees=degrees-360;}
double x;
double y;
double z;