Sign in to follow this  
ashenbert

how to make bullet shooting for shooting game in C++, OpenGL & SDL

Recommended Posts

I need help for making bullet shooting for shooting game in C++, OpenGL & SDL. I try to do it with these code (these are only part of the code): float mx; bool a; void Bullet() { glPushMatrix(); glTranslatef (0.5,0.5,mx); mx+=0.1; SolidSphere(0.5,20,20); glPopMatrix(); } //drawing function void RenderScene(bool &a,ObjLoader *PlayerPlane, GLuint textureName[2],Point3 EDir,Point3 PlaneT,bool hit,GLfloat radius2,vector<Sphere> SphereArray) { /*Clear the screen*/ glClearColor(0.1,0.1,0.1,1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); . . . a=false; if (a==true) { Bullet(); } } //function for user control void UserCtrl(Point3 &PlaneT, bool &a) { /*! \param UPDATE= the update of the Player's Plane when the player press the corresponding keys*/ #define UPDATE 0.5 //const unsigned int Bullet_interval=200; //unsigned in last_bullet_time=0; /*! \param oxupdate, oyupdate, ozupdate= the Player's plane update in x, y, z axis*/ float oxupdate=0.0; float oyupdate=0.0; float ozupdate=0.0; SDL_Event event; while( SDL_PollEvent( &event ) ) { switch( event.type ) { /* Look for a keypress */ case SDL_KEYDOWN: /*! Check the SDLKey values and move change the coords */ switch( event.key.keysym.sym ) { /*!Press 1 to have prespecive Orthographical Projection*/ case SDLK_1 : camera.SetProjection(); camera.SetupCamera(Point(-5,5,-5),Point(0,0,0),Point(0,1,0)); break; /*!Press 2 to have top view Orthographical Projection*/ case SDLK_2 : camera.SetProjection(); camera.SetupCamera(Point(0,5,0),Point(0,1,0),Point(0,0,1)); break; /*!Press 3 to have 90 degrees prespecive Projection*/ case SDLK_3 : camera.SetProjection2(90.0f,1024/768,1.0,50.0); camera.SetupCamera2(Point(0,1,-2),Point(0,1,0),Point(0,1,0)); break; //this is the key control for the bullet case SDLK_a : a=true; mx=0; break; ... } } //loop animation using SDL: while(1) { //DrawOpenGL(); RenderScene(a,PlayerPlane,textureName,EDir,PlaneT,hit,radius2,SphereArray); UserCtrl(PlaneT,a); /*Animation Update*/ Uint32 TimeNow=SDL_GetTicks(); if((TimeNow-FrameTime) >= 70) { Update(PlaneT,radius2,hit,SphereArray); Moveupdate(PlaneT); EDir.z-=0.005; FrameTime=SDL_GetTicks(); } SDL_Delay(2); SDL_GL_SwapBuffers(); } return EXIT_SUCCESS; } The result is when the program open, I can see a sphere moving in z axis(without pressing), after that when pressing 'a'each time, the sphere come out and move in z axis. My question is : how to make the sphere appear only when I press 'a'? how to make more spheres appear when pressing 'a' continuously? I hope that someone could help me to solve this. If you don't understand my code written here, please tell me so. Thank you. P.S. Thank you for the advices for my cloth simulation question, but unfortunately I cannot simulate a cloth for submission

Share this post


Link to post
Share on other sites
Please use the code-tags, it will make your message more readable.

In the RenderScene(), you are setting a = false and right after that you are testing if it's true, which it naturally never will be.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this