6 Replies - 881 Views - Last Post: 04 July 2012 - 08:19 PM Rate Topic: -----

#1 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

[SDL, C++] Shooting bullets in a SHMUP?

Posted 29 June 2012 - 10:48 AM

Hi!
How whould I write code so that when I hit space a bullet whould fly out a of the player.
I know how to write input and how to spawn the bullet, but how do I write the code so the bullet flys out of the player?
http://imgur.com/luLzj
Is This A Good Question/Topic? 0
  • +

Replies To: [SDL, C++] Shooting bullets in a SHMUP?

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1341
  • View blog
  • Posts: 3,513
  • Joined: 03-May 10

Re: [SDL, C++] Shooting bullets in a SHMUP?

Posted 29 June 2012 - 11:02 AM

Change the position of the bullet each from beginning from the point where the bullet is spawned, in the direction that you want it to travel. Are you just wanting a straight projectile? Something that curves? A homing projectile? Some more information would probably be good.
Was This Post Helpful? 0
  • +
  • -

#3 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

Re: [SDL, C++] Shooting bullets in a SHMUP?

Posted 29 June 2012 - 11:22 AM

View PostKilorn, on 29 June 2012 - 11:02 AM, said:

Change the position of the bullet each from beginning from the point where the bullet is spawned, in the direction that you want it to travel. Are you just wanting a straight projectile? Something that curves? A homing projectile? Some more information would probably be good.


How do I do so the bullet spawns at the player. I know how to make it fly forward.
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1341
  • View blog
  • Posts: 3,513
  • Joined: 03-May 10

Re: [SDL, C++] Shooting bullets in a SHMUP?

Posted 29 June 2012 - 11:38 AM

When you spawn the object, you set it's initial position to wherever the "gun" on the player's sprite is currently located. If the player's position is currently at (100, 100), and the gun is in the center of the player's sprite, you'll want to set the position of the bullet when it is spawned to (100 + playerWidth / 2, 100 + playerHeight / 2). This will create the bullet at the very center of the player object.
Was This Post Helpful? 1
  • +
  • -

#5 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

Re: [SDL, C++] Shooting bullets in a SHMUP?

Posted 04 July 2012 - 11:31 AM

Okay so I've got a bullet spawning at the player and all that is working but how do I do so you can't shoot until the bullet is out side of the window?
I know how to do a if statment, but how do I do this?
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 931
  • View blog
  • Posts: 4,012
  • Joined: 14-February 08

Re: [SDL, C++] Shooting bullets in a SHMUP?

Posted 04 July 2012 - 01:31 PM

Well think about it, try some pseudo code

if(bullet position < screen boundaries && player shooting)
{
    player able to shoot = false
}
else if(bullet position > screen boundaries && player shooting)
{
    player shooting = false
    player able to shoot = true
}



then when you listen for an event

if(shoot button pressed && player able to shoot)
{
    do shoot
}



When programming it is best for you to be able to explain the problem step by step and then break it down as needed then build it back up again.
Was This Post Helpful? 0
  • +
  • -

#7 ButchDean  Icon User is online

  • Ex-Pro Games Programmer
  • member icon

Reputation: 894
  • View blog
  • Posts: 3,383
  • Joined: 26-November 10

Re: [SDL, C++] Shooting bullets in a SHMUP?

Posted 04 July 2012 - 08:19 PM

I would prefer to give the physical properties over a series of if-statements and magic number. This would be a good opportunity to get to know vectors in 2D space, whereby this vector indicates the direction of travel.

You may be familiar with the force equation from classical mechanics:

Force = Mass * Acceleration

Not sure how savvy you are with physics, but as you can probably guess you increase force by either increasing mass, acceleration or both. Acceleration itself can be modeled as the effect of gravitational force acting in the vertical y-axis direction.

If you get your head around implementing this you can model a variety of projectiles that behave differently over time because even though mass is a constant, acceleration is the second derivative of velocity with respect to time.

Let me know if you are curious about further details.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1