Fire projectile

Started by
8 comments, last by CodeCriminal 13 years, 9 months ago
The problem is quite simple im making a game like space invaders and i m having a problem making my bullet, i dont know how to make when space is presses to make a new projectile and then check collision...So im asking how is the right way to do it....
Advertisement
What language are you using?


here is some sort of base for you to work with

class Bullet{public:	Vector3 CurrPos = Vector3.Zero();	float MovementAmount = 5f;	public void MoveBullet(int Amount)	{ 		CurrPos.Y - MovementAmount;	}//Constructor	public Bullet(Vector3 StartPos)	{		CurrPos = StartPos;	}}


This is a bullet, holding a position

public Bullet OnSpacePress(Vector3 SpawnPosition){	// Create a new Bullet at the spawn position	Bullet myNewBullet;	myNewBullet(SpawnPosition); //Create the bullet at the SpawnPosition}public void Update(){	//Move the bullets	for(int  i =0 ; i < bulletInBulletList; i++)		Bullet.MoveBullet();}public void CheckForCollisions(List Ships, List Bullets){	//Check each bullet in the list to see if it has has the same position as the Ship}


this is the kinda thing you wanna do, im not gunna write you game for you just give you a template. Let me know what lang your using and we can give you more help
As programmers, our skills reside in the ability to reduce a problem down into smaller, managable "bytes" (haha, snort.. meh)

So lets first begin with instancing a projectile, there are numerous ways we could go about doing this depending on the kind of projectile firing you want and various other factors.

But simplest and most Space Invaders-like would be to have a single bullet object which can be "re-fired" under two specific conditions;

1. The bullet has travelled to far and has gone beyond the "boundaries" of the stage.

2. The bullet has collided with something.

In either of these two cases, cease drawing the bullet and set a boolean (or 'bool') that can be used to determine whether or not the player is able to shoot.

May look like the following somewhat:
/* overly simplified codez */// .. player shootingif (canshoot){	canshoot = false;	bullet.x = player.x;	bullet.y = player.y;}// .. bullet updateif (!canshoot){	bullet.y -= 5;	if (bullet.y < 0 || collisionObject(bullet, invader)) // will probably want to loop through invaders array/vector seperatley 		canshoot = true;	drawObject(bullet);}



uhh.. brb dinner. :D (will edit when i get back..)
Wouldn't it actually be bullet.y += 5? In programming, the coordinates of the upper left corner are (0,0), so to move the bullet upwards it would have to get less negative. Also, the data should probably be encapsulated.
C++: Where your friends have access to your private members
Where is a face palm when i need it?
Quote:Original post by Fuji
Wouldn't it actually be bullet.y += 5? In programming, the coordinates of the upper left corner are (0,0), so to move the bullet upwards it would have to get less negative. Also, the data should probably be encapsulated.


Eh.. well that depends on how your game objects are oriented, but in the classic space invaders and in most clones of it, the player is situated at the bottom of the screen which is some number > 0 (0 being the top of the screen) and shooting upward.

@Burnt_Fyr - heres that facepalm you ordered *facepalm!*

EDIT: AND, which coordinate system convention you are using..? (center of the screen? lol weird!) - thanks jyk.

[Edited by - CodeCriminal on July 10, 2010 1:00:16 PM]
Quote:In programming, the coordinates of the upper left corner are (0,0)
That's entirely incorrect. Different graphics APIs use different coordinate system conventions, and with some APIs, multiple conventions are possible. For 2-d games, for example, common conventions are (0,0) in the upper-left and +y going down, (0,0) in the lower-left and +y going up, and (0,0) in the center of the screen.

In any case, it sounds like what you're talking about is a coordinate system where (0,0) is in the upper-left and +y goes up, which seems unusual (or at least is not something I've seen used often).
Ups i forgot im using c++ and for the graphicks SDL, i already made the function for drawing and movement the problem is when the use presses space to create a new object at its starting position

so
void missile_draw(){       apply_surface(Mx,My,shoot,screen);//just for now its not in the right position will fix later    --My;  }//colisionvoid game_collision(){  for(int j = 0; j < 3;++j)     {         for(int i = 0; i<10;++i)    {       if(SDL_CollidePixel(shoot,Mx,My,enemy1,enemies.x,enemies[j].y))       {         My = 300;                                                                      }            }       }                                   }//for the input if(event.type == SDL_KEYDOWN)   {      switch(event.key.keysym.sym)      {                 case SDLK_SPACE://make a bullet   break;                               } 


I now it doesnt yet check if its out of the screen i will just check if it is beyond the top side of the screen and if it is destroy it....

And i cant just call the function in the input bit because it wont render correctly

And i probably should put it in a struct or class for easier use?
Well dont take any notice of my code :P it was intended to give a simple idea behind what you want to do. I think i kinda mixed Languages C++ and C# hehe. as i didnt know what lang he was using
Quote:Original post by bequick
.. the problem is when the use presses space to create a new object at its starting position


That problem can be solved with the method provided in short above (my first post). Sorry I didnt finish it, I got bored.. Simply create a bullet (and by create i mean, provide suffiecent variables to define a bullet within your game environment :D ) during initialisation, and "move it around", to create the illusion that your player is firing multiple bullets (where as in reality he/she is repeatedly firing the same bullet, its just being repositioned under certain conditions)

Quote:Original post by bequickI now it doesnt yet check if its out of the screen i will just check if it is beyond the top side of the screen and if it is destroy it....


That is absolutley fine, no point in checking if the bullet goes out of the bottom of the screen ey?

Quote:Original post by bequickAnd i cant just call the function in the input bit because it wont render correctly.


I havent looked over your code.. properly, to come to any conclusion that might suggest such things.

Quote:Original post by bequickAnd i probably should put it in a struct or class for easier use?


Entirely up to you, I personally would find it very frustrating to not use any object based programming atall but thats because I came from gamemaker, a tiny bit of C# then C++ IMO (although, to be perfectly honest when I first started C++ I didn't really grasp classes straight away, and went for more 'C' like program structure, strange..)

The OOP paradigm is there because its the easiest way we know of (as humans) to manage larger projects, for smaller applications it doesnt really matter, but still good practice if you do decide to go with the object based stuffs.

Try to tackle the problem with the advice given, if not research it, every game programmer at some point in thier 'early years' has been faced with exact problem so there should be a lot of resources on it.

All the best!

[Edited by - CodeCriminal on July 10, 2010 3:40:58 PM]

This topic is closed to new replies.

Advertisement