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

Author Topic: Correct way to position Bullets shooting from ship  (Read 4352 times)

0 Members and 1 Guest are viewing this topic.

bryce910

  • Newbie
  • *
  • Posts: 31
    • View Profile
Correct way to position Bullets shooting from ship
« on: April 02, 2015, 07:05:24 am »
Hey everyone! I am back with my usual rotation questions! haha

I have got everything in terms of the angles working for the ship, bullets, and etc. Now I am facing what looks to be one last obstacle and I would love some ideas on how you guys would perform it if you were coding it.

Basically I have my sprite connected to my mouse in terms of rotation, so if I move my mouse to the right it rotates to the right, if I move my mouse to the left the sprite rotates to the left.  However, my issue is with the placement of the bullets leaving the cannons. My cannons are on the left and right wing, if the sprite is not rotated at all I have no issues and it is aligned perfect, but when I start to rotate the bullets don't quite align anymore. My code:



 float x = sprite.getGlobalBounds.left;
 float y = sprite.getGlobablBounds.top;
//shoot laser one
 float x = sprite.getGlobalBounds.left +sprite.getGlobalBounds().width;
//shoot laser two
 

LIke I said it works perfect when the sprite is not rotated, but as we rotate more the cannons and the start of the lasers don't quite align correctly.

Thanks again everyone!
« Last Edit: April 02, 2015, 07:20:48 am by bryce910 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Correct way to position Bullets shooting from ship
« Reply #1 on: April 02, 2015, 11:05:33 am »
I would love some ideas on how you guys would perform it if you were coding it.
For rotation and vector related functionality, I would use my library Thor, more concretely its Vectors module.

You can have vectors from the center of the ship to the cannon positions, let's say v for the right cannon. Usually, the cannon position is just p + v, where p is the ship's center position. When you rotate the ship by angle a (in degrees), p doesn't change, but v is rotated. The new cannon position is then
p + thor::rotatedVector(v, a) // rotate v by a°

Stop working with single coordinates X and Y, and trigonometry functions sin(), cos() etc. They are too low-level, you need too much code to achieve something simple. Vector algebra is far more powerful -- get used to operating directly on sf::Vector2f, not its components.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Correct way to position Bullets shooting from ship
« Reply #2 on: April 02, 2015, 11:08:15 am »
Sf::Transform works too, if you don't want to add thor to your project.

bryce910

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Correct way to position Bullets shooting from ship
« Reply #3 on: April 02, 2015, 03:13:55 pm »
Yea I would like to avoid adding thor or any other third party class to my system to be honest.  I use sin , cos, atan2 and etc. for all the angle stuff but I didn't think I would need it for something like setting the point of the laser.

I also am slightly confused on how sf::Transform would help me in this case?


Thanks! :-[

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Correct way to position Bullets shooting from ship
« Reply #4 on: April 02, 2015, 03:36:50 pm »
You can rotate the transform and then apply it to a point (the position of your cannon).

You'll find all the necessary information in the documentation, please ask more concrete questions if you're still stuck :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bryce910

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Correct way to position Bullets shooting from ship
« Reply #5 on: April 02, 2015, 03:53:07 pm »
Thank you nexus!

So I looked at the example and this is the code I have written. However, now it does move in the direction as I rotate my sprite but it is way off in the location aspect!


                        sf::Transform rot;
                        rot.rotate(airship.getRotation());
                        sf::Vector2f point = rot.transformPoint(Cannonx,Cannony);
 

So what this does is set my point and rotate it based off the rotation of the airship. This should allow me to also be aligned, but somewhere I either don't understand the basics of the transform class or I am jut an idiot! :p

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Correct way to position Bullets shooting from ship
« Reply #6 on: April 02, 2015, 04:03:52 pm »
Did you note how you never used the airship's position? That's the problem, you need to rotate the point around that center. There's another rotate() overload...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bryce910

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Correct way to position Bullets shooting from ship
« Reply #7 on: April 02, 2015, 05:51:21 pm »
Okay so I am extremely close to finally getting this. It is aligned everywhere (middle of the airship) except when my ship is facing right. Then the bullets fire from the top of the wing (Top of the sprite).

 airShip.setOrigin(airShip.getGlobalBounds().left + airShip.getGlobalBounds().width / 2, airShip.getGlobalBounds().top + airShip.getGlobalBounds().height / 2);
sf::Transform rot;
rot.rotate(airship.getRotation(), airship.getPosition().x,airship.getPosition().y);
sf::Vector2f point = rot.transformPoint(airship.getGlobalBounds().left + ( airship.getGlobalBounds().width / 2),airship.getGlobalBounds().top + airship.getGlobalBounds().height / 2);
laser.setPosition(point.x, point.y);

 

So I am guessing either my origin is not quite right, or something along the lines.

Thanks again for the help Nexus! I really appreciate it!   :D


--- EDIT ---

Output when I have it print out the information on the width, height, and origin I am getting for airship.

Width = 727
Height = 790
Origin = 67, 106
« Last Edit: April 02, 2015, 07:48:11 pm by bryce910 »

 

anything