1

I have a cannon that fires a cannonball and smoke particle effect, i want the cannon ball to start at the end of the cannon, this i can do by adding the width of the cannon to its x position and adding the half the height to the cannon. This works fine when the cannon is unrotated but when i rotate the cannon its not in the correct position. This is what i use to try and rotate the vector.

Vector2 rotPos = cannon.position.tmp().add(cannon.bounds.width, cannon.bounds.height/2).rotate(cannon.angle);

How can i get a rotated vector that fires the cannon ball in the correct place. See image below. enter image description here

UPDATE I tried the below also, same result the ball is off to the left

Vector2 rotPos = world.CannonBody.getWorldVector( world.CannonBody.getPosition() );
tsukimi
  • 1,605
  • 2
  • 21
  • 36
  • 1
    http://stackoverflow.com/questions/9164146/simple-gun-in-cocos2dbox2d-game See also the function GetWorldPoint which can give you the starting location – iforce2d Jan 20 '13 at 09:23
  • Im using `Vector2 rotPos = world.CannonBody.getWorldVector( world.CannonBody.getPosition() );` the ball does not shoot from the end of the cannon but to the left as in the image. Im rotating the cannon body using setTransform. – tsukimi Jan 21 '13 at 02:46
  • How can i get the end of the cannon body after rotation to shoot the ball from the end of the cannon? I have the correct angle and velocity , i just need to set the ball in the right position. – tsukimi Jan 21 '13 at 02:53
  • The following code works but only for angles between 0 and 45 degrees. `position.tmp().add(bounds.width * (float)Math.cos(Math.toRadians(angle)), bounds.width * (float) Math.sin(Math.toRadians(angle)) ); ` – tsukimi Jan 21 '13 at 09:42

2 Answers2

0

The way that you've described the problem, you've solved it for only a single case. This really is just a math problem. Think about the direction you want to shoot, the barrel of the cannon, as the coordinates on a circle.

Since you know the angle, this is easy. Draw a circle with a dot in the center. Then draw a line from the center to the right edge. Then draw another line at a 45 degree angle up from the first line. Connect the two points on the edges with a straight line. You have a triangle now.

Lets call the 45 degree angle line 'r'. And we'll call the first line x, and the last line y.

You should have something that looks like this: https://i.stack.imgur.com/MJNWZ.jpg

We know that sin(angleInRadians) = y/r. Doing a little algebra we can change this into r*sin(angleInRadians) = y

Boom, you have your y coordinate.

Almost the same thing: cos(angleInRadians) = x/r

So r*cos(angleInRadians) = x

There's your x coordinate.

The you can get the angle of a body directly from box2d, so that's easy. You just need to pick a value for 'r' that represents a correct radius for the circle that you're using to conceptualize the barrel of the cannon at a given angle. If the cannon rotates around the center of the circle, then r is the length of your cannon.

Scuba Steve
  • 1,541
  • 1
  • 19
  • 47
  • thanks for your answer. I havent looked at the code for a while, this question is a little old, but i am doing maths similar to what you posted. See my comment above – tsukimi Jul 16 '13 at 07:29
  • and it's still not working? that math should work for all real values of the angle theta, are you sure you're feeding it the right angle to begin with? If it's not that, then it could be that you've improperly converted from pixel coordinates to box2d coordinates. Also, you may want to consider which method you're using to position the cannon ball. If my memory serves me correctly, I use the setTransform method. – Scuba Steve Jul 17 '13 at 06:37
  • also, make sure that you're using the correct trig function for the x and y coordinates respectively, not just above, but in your code.. reversing them can make it not work – Scuba Steve Jul 17 '13 at 06:40
0

I had an issue which is similar to yours. Here's the question with an answer:

Android. How to move object in the direction it is facing (using Vector3 and Quaternion)

You need something like

translation.set(baseDirection).rot(modelInstance.transform).nor()
Community
  • 1
  • 1
Taras
  • 488
  • 6
  • 21