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

Author Topic: Spawning some bullets  (Read 3790 times)

0 Members and 1 Guest are viewing this topic.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Spawning some bullets
« on: September 09, 2014, 05:34:23 am »
So I have an idea on how I want to spawn bullets for this space-shooter I'm making. But the problem is, I have an option to rotate the shooter (setRotation + or -), and there's a specific bullet hole where the bullets are supposed to come out of.
I'm having trouble figuring out how to trace that bullet hole and set the bullet's position to that same spot even when the sprite rotates.
Can you guys please help me?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Spawning some bullets
« Reply #1 on: September 09, 2014, 06:06:04 am »
Basic example:

// initial location
bullet.x = cos(gun.getRotation()) * gun.getBarrelLength(); // cos: remember that most cos/sin impl use radians | barrelLength == radius
bullet.y = sin(gun.getRotation()) * gun.getBarrelLength(); // same things as above

Or if you also want to move the bullet in the same direction:

sf::Vector2f velocity(cos(gun.getRotation()), sin(gun.getRotation()));
// set initial position
bullet.setPosition(velocity * gun.getBarrelLength());

while (window.isOpen())
{
   // move each frame
   bullet.move(velocity * deltaTime * movementSpeed);
}

You should really read up on trigonometry and vector math, it really isn't optional when it comes to gamedev.
« Last Edit: September 09, 2014, 06:09:18 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Spawning some bullets
« Reply #2 on: September 10, 2014, 05:57:58 am »
Basic example:

// initial location
bullet.x = cos(gun.getRotation()) * gun.getBarrelLength(); // cos: remember that most cos/sin impl use radians | barrelLength == radius
bullet.y = sin(gun.getRotation()) * gun.getBarrelLength(); // same things as above

Or if you also want to move the bullet in the same direction:

sf::Vector2f velocity(cos(gun.getRotation()), sin(gun.getRotation()));
// set initial position
bullet.setPosition(velocity * gun.getBarrelLength());

while (window.isOpen())
{
   // move each frame
   bullet.move(velocity * deltaTime * movementSpeed);
}

You should really read up on trigonometry and vector math, it really isn't optional when it comes to gamedev.
I still don't quite get your example.
It starts from the top left of the screen  So you take the bullet set the position to the rotation of the barrel, for both x and y axis? Then multiply that by the length of the barrel? (Which in my case is 200,200)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Spawning some bullets
« Reply #3 on: September 10, 2014, 06:18:46 pm »
Please avoid using full quotes!

Quote
It starts from the top left of the screen

Well, you need to also add the gun's position to the bullet's starting position. You only asked how to calculate the position of the bullet depending on the rotation........


Quote
So you take the bullet set the position to the rotation of the barrel, for both x and y axis?

No you don't set the position to the rotation, you set it to the sine and cosine of the rotation. As I already said, you need to understand trigonometry if you wish to understand why this code works.

http://www.mathsisfun.com/sine-cosine-tangent.html

Quote
(Which in my case is 200,200)

How can it be {200, 200}? Length by definition is only 1 dimension.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Spawning some bullets
« Reply #4 on: September 11, 2014, 01:00:46 am »
Please avoid using full quotes!

Quote
It starts from the top left of the screen

Well, you need to also add the gun's position to the bullet's starting position. You only asked how to calculate the position of the bullet depending on the rotation........







How can it be {200, 200}? Length by definition is only 1 dimension.
I asked how to get the position of the barrel (the barrel moves upon rotation of the sprite) and set the bullet's position to the tip of the barrel, no matter what rotation the barrel is at.

And I was referring to my whole sprite when I said 200,200  but the length of the barrel is smaller.  I'm not on mu pc right now so I can't tell you.

So to clarify again, I want to know how to get the tip of the barrel's position,  so I can set it as the bullet starting position.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Spawning some bullets
« Reply #5 on: September 11, 2014, 03:12:40 am »
..............

Quote
So to clarify again, I want to know how to get the tip of the barrel's position,  so I can set it as the bullet starting position.

Read my first post, it already is answered. And if it doesn't make sense, read the link in my second post.

(click to show/hide)
« Last Edit: September 11, 2014, 03:35:52 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

danharibo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Spawning some bullets
« Reply #6 on: September 11, 2014, 03:48:48 am »
So I have an idea on how I want to spawn bullets for this space-shooter I'm making. But the problem is, I have an option to rotate the shooter (setRotation + or -), and there's a specific bullet hole where the bullets are supposed to come out of.
I'm having trouble figuring out how to trace that bullet hole and set the bullet's position to that same spot even when the sprite rotates.
Can you guys please help me?

Based on your description, I read your problem as follows:

You have a shooter, with a position and a rotation.
You want to create bullets at a position on the shooter that changes with it's rotation.

You can do that quite simply with a Rotation Matrix, which is quite simple for 2D rotations. For example, if your gun's barrel is at {10, 20} relative to the centre of the shooter sprite, then you can calculate where the bullet should be created with:
float rotation = shooter.getRotation();
sf::Vector2f barrel(10.f, 20.f);
sf::Vector2f rotated;
// Rotate barrel around {0,0}.
rotated.x = barrel.x * cos(rotation) - barrel.y * sin(rotation);
rotated.y = barrel.x * sin(rotation) + barrel.y * cos(rotation);
// "barrel" is relative to the center of the sprite, we must add it to the position to get the final coordinates.
sf::Vector2f barrelWorld = shooter.getPosition() + rotated;

disclaimer: I haven't tried to run this code.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Spawning some bullets
« Reply #7 on: September 12, 2014, 12:52:40 am »
Okay, thank you everyone.
Very helpful!