5 Replies - 15318 Views - Last Post: 22 April 2009 - 11:27 AM Rate Topic: -----

#1 Rohtie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 07-March 07

2d shooter - shooting bullets [sdl + opengl]

Posted 19 April 2009 - 07:16 AM

I have searched alot on google about how to make a bullet that goes with the angle of my turret.
This is the code i have for the bullet... There is something very wrong with it, the bullet seems like it wants to fly to somewhere random. I use sdl + opengl.
Bullet::Bullet(){
	speed = 2;
	//velocity
	xvel = 0;
	yvel = 0;
	//mouse coordinates
	mx = 0;
	my = 0;
	//offset
	x = SCREEN_HEIGHT/2;
	y = SCREEN_WIDTH/2;
	hyp = 0;
	shoot = 0;
	life = 100;
}

void Bullet::Hevent(){
	//If the mouse moved

	if( event.type == SDL_MOUSEMOTION ) {

	//Get the mouse offsets

	mx = event.motion.x;

	my = event.motion.y;
	}
	//if left mouse button was pressed
	if(event.type == SDL_MOUSEBUTTONDOWN){
	if(event.button.button == SDL_BUTTON_LEFT){
		//hyp = sqrt(pow(mx-x,2)+pow(my-y,2);
		//debug
		printf ("x: %d y: %d mousex: %d mousey: %d Angle: %f \n", x, y, mx, my, Angle);
		shoot = 1;
	}}
}

void Bullet::move(){
	int Radians;
	if(shoot == 1){
		Radians = Angle * PI / 180;
		xvel = speed * cos(Radians);
		yvel = speed * sin(Radians);
		x -= xvel;
		y -= yvel;

/*		xvel = floor(x * speed / hyp);
		yvel = floor(y * speed / hyp);
		x -= xvel;
		y -= yvel;*/
		life--;
	}
	if(life == 0){
		shoot = 0;
		life = 100;
	}
}

void Bullet::show(){
	//Move to offset

	glTranslatef( x, y, 0 );


	//Start quad

	glBegin( GL_QUADS );



	//Set color to white

		glColor4f( 1.0, 1.0, 1.0, 1.0 );



		//Draw bullet

		glVertex3f( 0, 0, 0 );

		glVertex3f( 5, 0, 0 );

		glVertex3f( 5, 5, 0 );

		glVertex3f( 0, 5, 0 );



	//End quad

	glEnd();



	//Reset

	glLoadIdentity();
}



It would be helpful if you give me some mathematic formulas or something simelar.

This post has been edited by Rohtie: 19 April 2009 - 07:17 AM


Is This A Good Question/Topic? 0
  • +

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: 2d shooter - shooting bullets [sdl + opengl]

Posted 19 April 2009 - 10:27 PM

The only thing you really need to keep track of is the "Angle" and "Speed". Once you've got those two variables, you plug them into a formula and simply change the X and Y values.

void MoveBullet() {
	x += std::cos( Direction*( PI/180 ) ) * Speed;
	y += std::sin( Direction*( PI/180 ) ) * Speed;
}


Was This Post Helpful? 2

#4 Rohtie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 07-March 07

Re: 2d shooter - shooting bullets [sdl + opengl]

Posted 20 April 2009 - 10:50 AM

Thanks alot! This solved my problem :)
I have one more problem... How do I, or what is the best way to shoot multiple bullets?

This post has been edited by Rohtie: 20 April 2009 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: 2d shooter - shooting bullets [sdl + opengl]

Posted 20 April 2009 - 10:53 AM

The best way to shoot multiple bullets would be to first create a "Bullet" class.

Then, create a container (array, vector, etc.) that stores "Bullet" objects.

In the "shoot event", create a bullet object and store the reference into your container.

Create a loop that updates, draws, etc. all of the bullet instances stored in the container.

Regards,
Emmanuel Barroga
Was This Post Helpful? 0
  • +
  • -

#6 Rohtie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 07-March 07

Re: 2d shooter - shooting bullets [sdl + opengl]

Posted 21 April 2009 - 11:44 AM

Thanks for your directions. I tried to do something simelar, but something went wrong. A lot of bullets appear at random places in my window when I shoot for the first time.
class bullet{
	private:
	int container[128][3];
	int speed, id;

	public:
	bullet();
	void create();
	void move();
	void draw();
};

bullet::bullet(){
	speed = 5;
	id = 0;
}

void bullet::create(){
	if(docreate == 1){
		container[id][0] = bx;
		container[id][1] = by;
		container[id][2] = dir;
		printf("id: %d, bx: %d, by: %d, direction: %d",id,bx,by,dir);
	}
}

void bullet::move(){
	if(id > 0 || docreate == 1){
		for(int l=0; l < id; l++){
			container[l][0] += cos(container[l][2] * (PI/180)) * speed;
			container[l][1] += sin(container[l][2] * (PI/180)) * speed;
		}
	}
}

void bullet::draw(){
	if(id > 0 || docreate == 1){
		for(int l=0; l < id; l++){
			glTranslatef(container[l][0], container[l][1], 0);
			glRotatef(container[l][2],0,0,1); glTranslatef(-2.5,-50,0);

			glBegin(GL_QUADS);

				glVertex3f(0, 0, 0);

				glVertex3f(5, 0, 0);

				glVertex3f(5, 10, 0);

				glVertex3f(0, 10, 0);

			glEnd();

			glLoadIdentity();
		}
	}
	if(docreate == 1){
	id++;
	docreate = 0;	
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: 2d shooter - shooting bullets [sdl + opengl]

Posted 22 April 2009 - 11:27 AM

The first problem that I noticed in your class is that each bullet instance will have their own, separate "container". The container should be global, although since global variables is bad practice you should declare the container static to the class. Also, the container should hold Bullet types:
class Bullet {
	static Bullet Container;
};



I'm not sure if you're familiar with vectors but you might want to use it over arrays. It's simply an array with management built-in.

vector<Bullet> Container;



I hope this post was helpful!
Was This Post Helpful? 1

Page 1 of 1