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
[SDL, C++] Shooting bullets in a SHMUP?
Page 1 of 16 Replies - 881 Views - Last Post: 04 July 2012 - 08:19 PM
Replies To: [SDL, C++] Shooting bullets in a SHMUP?
#2
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.
#3
Re: [SDL, C++] Shooting bullets in a SHMUP?
Posted 29 June 2012 - 11:22 AM
Kilorn, 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.
#4
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.
#5
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?
I know how to do a if statment, but how do I do this?
#6
Re: [SDL, C++] Shooting bullets in a SHMUP?
Posted 04 July 2012 - 01:31 PM
Well think about it, try some pseudo code
then when you listen for an event
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.
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.
#7
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|