Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / 3d Bullet Function in C++

Author
Message
tag104367
12
Years of Service
User Offline
Joined: 1st Feb 2012
Location:
Posted: 2nd Feb 2012 00:46
I have been working at this and i can't find any tutorials im making a simple shoot function for my game but when i run it works if you shoot extremely slow and the bullets delete and everything but if you shoot it fast then it shoots one bullet then creates the next bullet infront of the last bullet and doesn't update the bullets idk what it is i have tried to do everything heres my code for the shooting part

void Shoot(){
dbLoadImage("GunShot.png", 12);
dbMakeObjectBox (11, 7 , 7, 0);
dbPositionObject(11, dbObjectPositionX(1)-2, dbObjectPositionY(1)+8, dbObjectPositionZ(1)+24);
dbTextureObject (11 , 12);
dbSetObjectLight (11 , 0);
dbSetObjectEmissive(11 , 0x00646464);
dbSetObjectAmbience (11 , 0x00FFFFFF);
dbFadeObject(11, 8000);
dbSetObjectTransparency(11 , 1);
dbHideObject(11);
dbSetObjectCollisionOff(11);
dbLockObjectOn(11);
dbLoadSound("gun1.wav", 15);
dbPauseSound(15);
dbSetSoundSpeed(15,5000);

UpdateBullet();

if(bullets > 0){
if(dbMouseClick() == 1){
if(dbTimer() - ShootingTimer > 160){
if (BulletCount >= 30) {
BulletCount = 0;
}
bullets--;
Bullet();
dbPlaySound(15);
dbShowObject(11);
dbRollObjectLeft(11,(float)(rand() % 360));
dbShowLight(0);
dbPositionLight(0,dbObjectPositionX(11), dbObjectPositionY(11), dbObjectPositionZ(11));
}
}
}



if(bullets < 1){
dbText(200,400, "PRESS R TO RELOAD");
}
if (dbKeyState(19) == 1){
reload();
}

}


void reload(){
y = 30 - bullets;
for(int i = 0; i < y; i++){
bullets++;

}
}


void Bullet(){
for (int i = 0; i < 30; i++){
BULLET[i][1] = BulletCount;
dbMakeObjectSphere(BULLET[i][1]+300,3);
}
BULLET[BulletCount][3] = dbTimer();
BULLET[BulletCount][2] = 1;
BULLET[BulletCount][4] = 1400;

dbPositionObject(BULLET[BulletCount][1]+300, dbCameraPositionX()+5, dbCameraPositionY(), dbCameraPositionZ()+10);
dbXRotateObject(BULLET[BulletCount][1]+300, fCameraAngleX);
dbYRotateObject(BULLET[BulletCount][1]+300, fCameraAngleY);

dbPositionObject(BULLET[BulletCount][0]+300, dbCameraPositionX() + 5, dbCameraPositionY(), dbCameraPositionZ()+10);
dbXRotateObject(BULLET[BulletCount][0]+300, fCameraAngleX);
dbYRotateObject(BULLET[BulletCount][0]+300, fCameraAngleY);
BulletCount++;
}

void UpdateBullet(){
for (int i = 0; i < 30; i++){
if (dbObjectExist(BULLET[i][1]+300)){
dbMoveObject(BULLET[i][0]+300, BulletSpeed);
dbMoveObject(BULLET[i][1]+300, BulletSpeed);
}
}
}




i know the code is bad im just making a rough thing im going to use classes and everything when i make the real thing im just messing around and trying to get a feel for dark gdk with c++


any help would be greatly appreciated thanks in advance
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Feb 2012 01:54
First of all I don't see a loop.....

I don't even know what you are trying to do here:

This will make 1 bullet 30 times.

The fastest code is the code never written.
tag104367
12
Years of Service
User Offline
Joined: 1st Feb 2012
Location:
Posted: 2nd Feb 2012 01:57
sorry when i copy paste it must have left out part it should be

for (int i = 0; i < 30; i++){
BULLET[i][1] = BulletCount;
dbMakeObjectSphere(BULLET[i][1]+300,3);
}
tag104367
12
Years of Service
User Offline
Joined: 1st Feb 2012
Location:
Posted: 2nd Feb 2012 02:46
ok i dont know why but the code that i copy paste left all the [i]s out so im going to re post it void Shoot(){
dbLoadImage("GunShot.png", 12);
dbMakeObjectBox (11, 7 , 7, 0);
dbPositionObject(11, dbObjectPositionX(1)-2, dbObjectPositionY(1)+8, dbObjectPositionZ(1)+24);
dbTextureObject (11 , 12);
dbSetObjectLight (11 , 0);
dbSetObjectEmissive(11 , 0x00646464);
dbSetObjectAmbience (11 , 0x00FFFFFF);
dbFadeObject(11, 8000);
dbSetObjectTransparency(11 , 1);
dbHideObject(11);
dbSetObjectCollisionOff(11);
dbLockObjectOn(11);
dbLoadSound("gun1.wav", 15);
dbPauseSound(15);
dbSetSoundSpeed(15,5000);

UpdateBullet();

if(bullets > 0){
if(dbMouseClick() == 1){
if(dbTimer() - ShootingTimer > 160){
if (BulletCount >= 30) {
BulletCount = 0;
}
bullets--;
Bullet();
dbPlaySound(15);
dbShowObject(11);
dbRollObjectLeft(11,(float)(rand() % 360));
dbShowLight(0);
dbPositionLight(0,dbObjectPositionX(11), dbObjectPositionY(11), dbObjectPositionZ(11));
}
}
}



if(bullets < 1){
dbText(200,400, "PRESS R TO RELOAD");
}
if (dbKeyState(19) == 1){
reload();
}

}


void reload(){
y = 30 - bullets;
for(int i = 0; i < y; i++){
bullets++;

}
}


void Bullet(){
for (int i = 0; i < 30; i++){
BULLET[i][1] = BulletCount;
dbMakeObjectSphere(BULLET[i][1]+300,3);
}
BULLET[BulletCount][3] = dbTimer();
BULLET[BulletCount][2] = 1;
BULLET[BulletCount][4] = 1400;

dbPositionObject(BULLET[BulletCount][1]+300, dbCameraPositionX()+5, dbCameraPositionY(), dbCameraPositionZ()+10);
dbXRotateObject(BULLET[BulletCount][1]+300, fCameraAngleX);
dbYRotateObject(BULLET[BulletCount][1]+300, fCameraAngleY);

dbPositionObject(BULLET[BulletCount][0]+300, dbCameraPositionX() + 5, dbCameraPositionY(), dbCameraPositionZ()+10);
dbXRotateObject(BULLET[BulletCount][0]+300, fCameraAngleX);
dbYRotateObject(BULLET[BulletCount][0]+300, fCameraAngleY);
BulletCount++;
}

void UpdateBullet(){
for (int i = 0; i < 30; i++){
if (dbObjectExist(BULLET[i][1]+300)){
dbMoveObject(BULLET[i][0]+300, BulletSpeed);
dbMoveObject(BULLET[i][1]+300, BulletSpeed);
}
}
}
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Feb 2012 04:04
Use the "code" button at the top right of the "Post Forum Message" box you enter your post. Highlight the code once you paste it in the box and then hit the "code" button. That will put it in the format like this:



The fastest code is the code never written.
tag104367
12
Years of Service
User Offline
Joined: 1st Feb 2012
Location:
Posted: 2nd Feb 2012 05:03
ok so heres the code lol

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Feb 2012 17:40

One of your problems is here:

This will create 1 bullet 30 times (which will fail after the first one), when you fire the first time. The second time you fire, it will create 1 bullet 30 times (and fail just like the first time). You need to change the code some. I don't know what your "bullet dead/not created" code is, but you should make this section something like this:


The fastest code is the code never written.
tag104367
12
Years of Service
User Offline
Joined: 1st Feb 2012
Location:
Posted: 3rd Feb 2012 04:41
when i run it it only looks like it is creating one bullet every click the weird thing is it runs perfectly fine when i do one click at a time but as soon as i hold it it it just creates the next bullet infront of the last i attached a print screen of whats happening i tried what u said and it didn't change anything from what i could tell

in the print screen the red circles are circling the bullets its a pic from the left after i shot and stopped shooting idk if this would be from the creation function or the update function its almost as if the update function stops updating the bullets

Attachments

Login to view attachments
tag104367
12
Years of Service
User Offline
Joined: 1st Feb 2012
Location:
Posted: 3rd Feb 2012 04:50
OMG NEVER MIND THANK YOU LOL!!!! i fixed it i changed


to


my new problem is that for some reason they dont update until i have let go of the left click if i hold it they just build up one on front of the other and then i release and they move as a big group or a small group depending on how long you hold click for do you know why this would happen?
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 3rd Feb 2012 12:55
You need to put a timer in your code. What's happening is that you are creating a huge number of bullets (but a max of 30) and resetting their position to the start position as long as you hold down the fire key.
You have "ShootingTimer" but you are not setting it to anything. Right after this:

set "ShootingTimer=dbTimer();"

The fastest code is the code never written.

Login to post a reply

Server time is: 2024-04-19 05:09:04
Your offset time is: 2024-04-19 05:09:04