Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Calculate start position for shooting bullet from the ship.  (Read 2044 times)

0 Members and 1 Guest are viewing this topic.

aratnon

  • Newbie
  • *
  • Posts: 24
    • View Profile
Calculate start position for shooting bullet from the ship.
« on: September 01, 2013, 08:14:19 am »
Hi guys, I try to make a ship shoot a bullet toward a mouse position with this code
float deltaX = mousePosition.x - (aircraft.getPosition().x + aircraft.getLocalBounds().width / 2);
float deltaY = mousePosition.y - (aircraft.getPosition().y + aircraft.getLocalBounds().height / 2);
float angleRadian = (float)(std::atan2(deltaY, deltaX));
float angleDegree = (float)(angleRadian * 180 / PI);

It works fine with one bullet but when I add another bullet I can't figure out how to calculate the start position for each bullets.

This is a picture of this problem.


I try to specify start position of each bullets by this code.
sf::Vector2f offset1(aircraft.getPosition());
offset1.x += -10;//start shooting point for first bullet.

sf::Vector2f offset2(aircraft.getPosition());
offset2.x += 10;//start shooting point for second bullet.

But when I rotate the ship to other angles the bullets are not the same as the first picture.
How can I calculate the start position of each bullets? :)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Calculate start position for shooting bullet from the ship.
« Reply #1 on: September 01, 2013, 09:05:28 am »
It's a little hard to tell, but I assume what you're asking is how can you offset the bullets ten pixels in a direction perpendicular to the ship's direction, rather than simply ten pixels left and right.

Basically, trig functions.  For this task it'll probably be as simple as using 10*sin(ship_direction) for the y offset and 10*cos(ship_direction) for the x offset, or something similar.  I'll let you work out the exact code from that hint.

You're probably going to run into a lot of problems like this, so I'd recommend putting some time and effort into understanding how all these trig functions actually work so you can write your own formulas the next time you need to adjust things based on angles.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Calculate start position for shooting bullet from the ship.
« Reply #2 on: September 01, 2013, 06:24:56 pm »
Note that he also asked the question on gamedev.stackexchange.