Jump to content
  • Advertisement
Sign in to follow this  
HelderBorges

noob bullet question

This topic is 2595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys. i´m a little tied with a problem.
I want to make a bullet engine, where i have a tower and a hero. When the hero enter the zone of fire, the tower should shoot in the first position of the hero.
My problem for now is how to move the bullet in the right direction. Can someone help?

I´m using c/c++ with visual studio and OPENGL.

here is a little of my code


float xpto = 0;
.
.
.
This calc the distance between the tower and the hero and start the bullet


float d =calcdistance(post,posb);
if(d<=5)
{
if(bala == false){
destino[0]=posb[0];
destino[1]=posb[1];
destino[2]=posb[2];
origem[0]=post[0];
origem[1]=post[1];
origem[2]=post[2];
drawBullet();
}
if(bala == true){
drawBullet();}
glRotatef(atan2(post[0]-posb[0],post[2]-posb[2])*(180/3.14),0,1,0);

}
.
.
.

where origem[3] and destino[3] are arrays to save the X,Y,Z from the first place of the hero (the place that bullet should hit) and the start of the buller (first tower cordinates).


.
.
.

the bullet function.


void drawBullet(){
bala = true;

glPushMatrix();
glTranslatef(0,1,0);
glRotatef(atan2(origem[0]-destino[0],origem[2]-destino[2])*(180/3.14),0,1,0);
glTranslatef(xpto,0,xpto);
glColor3f(0.8f, 0.5f, 2.0f);
glutSolidSphere(0.6,30,30);
float d =calculardistancia(origem,destino);
if(d==0){
bala= false;}
xpto+=0.01;
glPopMatrix();

}





Can someone help?

If you have better tips for code i wold appreciate too :P

this is my bullet movement for now : glTranslatef(xpto,0,xpto); But it isnt working well.... :S obviously the bullet go in the same direction always :S but i dont know how to make her go in the right direction,,,,,

Share this post


Link to post
Share on other sites
Advertisement
What is xpto in your code? Could we see a larger section of your code, it's hard to really see what is going on from what you posted.

The typical way of making something travel toward between two points is to establish a vector from the origin point to the destination point. You can do this by subtracting the origin point from the destination. something like this:


TravelVec[0] = Dest[0] - Origin[0];
TravelVec[1] = Dest[1] - Origin[1];
TravelVec[2] = Dest[2] - Origin[2];


Now you know the direction that the bullet needs to travel.

Share this post


Link to post
Share on other sites

What is xpto in your code? Could we see a larger section of your code, it's hard to really see what is going on from what you posted.

The typical way of making something travel toward between two points is to establish a vector from the origin point to the destination point. You can do this by subtracting the origin point from the destination. something like this:


TravelVec[0] = Dest[0] - Origin[0];
TravelVec[1] = Dest[1] - Origin[1];
TravelVec[2] = Dest[2] - Origin[2];


Now you know the direction that the bullet needs to travel.


I will update the code. And if i want the bullet to travel? just need to had like TravelVec[0]+0.01?

Share this post


Link to post
Share on other sites
This is my code from the towers and the bullet.





bool bala=false; // verify if the bullet is created
float xpto=0; //random variable to move the bullet :D
float destino[3]; // destiny position
float origem[3]; // start position
float posbullet[3]; // bullet position




void drawBala(){
bala = true;
float d1 =calculardistancia(destino,origem); // calc dist between destiny and origin
float d2 =calculardistancia(posb,posbullet);// calc dist between hero and bullet
printf("###########-Initial distance %f-#########\n",d1); // only to know the distance
printf("**********-Bullet distance %f-**********\n",d2);// only to know the distance
if(d1>5||(d2>0.1&&d2<-0.1)){ // condition to stop drawing the bullet if bullet reached the target or passed the range
bala= false;
}
else{
glPushMatrix();
glTranslatef(0,1,0);
glRotatef(atan2(origem[0]-destino[0],origem[2]-destino[2])*(180/3.14),0,1,0); // rotate bullet to target
glTranslatef(0,0,xpto); // translate in the z axys (dont know why but si working...lol)
posbullet[2]+=xpto; // update bullet position
glColor3f(0.8f, 0.5f, 2.0f);
glutSolidSphere(0.6,30,30);
xpto+=origem[2]-(origem[2]+0.1); // incress the movement
glPopMatrix();
}


}


void drawTorre(float m,float rtorre,float n){


glColor3f(1.0f, 2.0f, 0.0f);

glPushMatrix();


glTranslatef(0, coiso*distancia*escala,-10);
post[0]=0;
post[1]= 0;
post[2]=-10;
float d =calculardistancia(post,posb);
if(d<=5)
{
if(bala == false){ // if ther is no bullet draw a new one
glPushMatrix();
destino[0]=posb[0];
destino[1]=posb[1];
destino[2]=posb[2];
origem[0]=post[0];
origem[1]=post[1];
origem[2]=post[2];
posbullet[0]=origem[0];
posbullet[1]=origem[1];
posbullet[2]=origem[2];
drawBala();
glPopMatrix();
}
if(bala == true){ // if bullet is already draw, continue to draw it.
drawBala();
printf("#########-Bullet on-###########\n");}
printf("vvvvvvvv-%f-vvvvvvv\n",d);
glRotatef(atan2(post[0]-posb[0],post[2]-posb[2])*(180/3.14),0,1,0);

}

for(int i=0;i<ndonuts;i++){
glPushMatrix();
glRotatef(90,1*escala,0,0);
glutWireTorus(raiointrior,raioexterior,numlados,aneis);
glPopMatrix();
glTranslatef(0, coiso*distancia*escala, 0);
}

//Bule de chá
glTranslatef(0, coiso*distancia*escala-0.2, 0);
glRotatef(90,0,1,0);
glRotatef(-45,0,0,1*escala);//Incliar bool
glColor3f(0.0f, 1.0f, 0.0f);
glutSolidTeapot(tamanhobule); //Cria bule
glPopMatrix();



}











The bullet is already moving in the right direction with this code, but i cant make it stop :S

Share this post


Link to post
Share on other sites
Now i have the bullets on the move, and on the right direction, but i cant detect collisions.

My code now is this:



bool bala=false;
float xptocontador=0; // var to count number of movements of the bullet
float destino[3];
float origem[3];
float posbullet[3];


float calculardistancia(float *a,float*b){ // function to calc dist

return (sqrt(pow(abs(a[0])-abs(b[0]),2)+pow(abs(a[2])-abs(b[2]),2)));
}


void drawBala(){
bala = true;
float d1 =calculardistancia(destino,posbullet); // variable to calculate distance between final destiny and bullet
float d2 =calculardistancia(posb,posbullet);// variable to calculate distance between hero and bullet
printf("###########-pos boneco %f %f %f-########\n",posb[0],posb[1],posb[2]); //cheeking experience
printf("**********dist bala %f -**********\n",d2); //cheeking experience
if((int ) xptocontador==5||calculardistancia(posb,posbullet)==0){ // should stop drawing the bullet to start a new one if hit the hero or reached the end of the range
bala= false;
xptocontador=0;
}
else{
glPushMatrix();
glTranslatef(0,1,0);
glRotatef(atan2(post[0]-destino[0],post[2]-destino[2])*(180/3.14)-0.8*(180/3.14),0,1,0);
glTranslatef(post[0]-destino[0]-((post[0]-destino[0])+xptocontador),0,post[2]-destino[2]-((post[2]-destino[2])+xptocontador)); // Move the bullet for the position
glColor3f(0.8f, 0.5f, 2.0f);
glutSolidSphere(0.6,30,30);
printf("ttttttttttt-xpto %f-tttttttttttttttttttt\n",xptocontador);
glPopMatrix();
xptocontador+=0.01;
posbullet[0]=post[0]-destino[0]-((post[0]-destino[0])+xptocontador); // calc the position of the bullet (dont know if it is the right way...)
posbullet[1]=post[1]; // calc the position of the bullet (dont know if it is the right way...)
posbullet[2]=post[2]-destino[2]-((post[2]-destino[2])+xptocontador); // calc the position of the bullet (dont know if it is the right way...)
printf("###########-pos bullet %f %f %f-########\n",posbullet[0],posbullet[1],posbullet[2]);
}


}



void drawTorre(float m,float rtorre,float n){


glColor3f(1.0f, 2.0f, 0.0f);

glPushMatrix();


glTranslatef(0, coiso*distancia*escala,-10);
post[0]=0;
post[1]= 0;
post[2]=-10;
float d =calculardistancia(post,posb);
if(d<=5)
{
if(bala == false){
glPushMatrix();
destino[0]=posb[0];
destino[1]=posb[1];
destino[2]=posb[2];
drawBala();
glPopMatrix();
}
}
if(bala == true){
drawBala();
printf("#########-Bullet on-###########\n");}
printf("vvvvvvvv-%f-vvvvvvv\n",d);
glRotatef(atan2(post[0]-posb[0],post[2]-posb[2])*(180/3.14),0,1,0);
for(int i=0;i<ndonuts;i++){
glPushMatrix();
glRotatef(90,1*escala,0,0);
glutWireTorus(raiointrior,raioexterior,numlados,aneis);
glPopMatrix();
glTranslatef(0, coiso*distancia*escala, 0);
}

//Bule de chá
glTranslatef(0, coiso*distancia*escala-0.2, 0);
glRotatef(90,0,1,0);
glRotatef(-45,0,0,1*escala);//Incliar bool
glColor3f(0.0f, 1.0f, 0.0f);
glutSolidTeapot(tamanhobule); //Cria bule
glPopMatrix();



}





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!