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 / How to create multiple bullets

Author
Message
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 28th Dec 2011 21:43
Im remaking my top down 2d shooter.

This time i want to make it right so im trying to make a shoot system that its efective.

The question is: How can i make a bullet's array using my structure and then indentify if one is alive or not and stuff so there can be more than 1 bullet in the game




C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 29th Dec 2011 02:39
Well, first I think you mis-typed, but yes it can be done. I have been toying with the idea of "linked lists". This is where you have an indetermined amount of "stuff". There are two solution I know of: Make your array HUGE or use a linked list.

Here is an example of what you could use:


The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 29th Dec 2011 11:39
Hum seems nice

I never learn this lists, but i will try to implement them in my game with your explanation i think i can.

Just one thing, what is the float D and float S in make function?

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 29th Dec 2011 15:34
Quote: "Just one thing, what is the float D and float S in make function?"
Each bullet has a Direction (D) and a Speed (S). I assumed by your first post that you had a 2D game in mind, so the direction only needed to be 0-360. Since some bullets would have different speeds, I gave you a speed variable.

I'll keep monitoring this thread if you have any questions.

The fastest code is the code never written.
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 29th Dec 2011 21:25
Found a problem. I didn't discuss how to actually destroy an individual bullet within the BULLET::MoveBullets() call.
Here is the updated code with comments:

Without this, the pointer "Next" would have no value and dump out after the first destroyed object. "OldNext" preserves that value so the chain can continue.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 29th Dec 2011 21:30 Edited at: 29th Dec 2011 22:26
nice nice, so to destroy on bullet i only need bullet.destroy() function.. but wait.. how can i know what bullet number is it?

@edit
finally got home and implemented the code in the game.

So i will make the bullet go to the location of the mouse (of course only take that direction) and then move on, when get out of screen destroy it. But im not understanding the definition of the direction.. :S

im kinda slow

@edit2
i still waNt to know the first edit question, but another question how to properly call a shot? :S
And whats the TestTheBullets(void)

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 29th Dec 2011 23:57
TestTheBullets(void) is just a function I made to give examples of how to use the BULLET struct.
Quote: "But im not understanding the definition of the direction"
Now I'm a little confused.... (Please don't think I'm talking down to you) When you have 2 points in space (or on the screen) and you draw an arrow (imaginary) from one point to the other, that arrow has a direction. It can be expressed as a ratio like y/x such as the slope of a line (Y=mX+b where X & Y are coordinates on the screen b is the Y-intercept and m is your slope). It can also be expressed as an angle. The angle can be calculated by finding the arctangent of the two points (DGDK has a function that does the math for you). Here is an example:

There is another thing you should change in the code I gave you. Here is the new BULLET struct:

I changed the "int x,y;" to "float x,y;". This is needed to make the bullet path consistant because each frame your x,y values will change slightly, and unless you are shooting exactly on either the X or Y axis, the bullet will not go in a straight line with integer values (trust me).

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 30th Dec 2011 00:21
No problem, i know is confusing what im asking :S sorry
And i used that function the Atanfull before, but previous uses i needed to inform darkgdk that the direction (the final x and y) was the MouseX and MouseY just that..

I dont know if your code is ready for it or not, thats what i asked

Ok now i got all setup
But i want to create the actual shot inside the player Update function

i got:
void Player::Update()
{
//handle movement and collision
//handle shooting
//what to actually put here?
}

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 30th Dec 2011 01:03
Inside your "Player" struct you need to declare the first and last bullet:

I hope you understand my psudo-code. If not, I can give you a full implimentation if you give me your "Player" struct declaration.

The fastest code is the code never written.
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 30th Dec 2011 02:35
If I remember correctly linked lists are slower to traverse than arrays due to arrays using blocks of memory and linked lists using different chunks in different areas. It's not terribly significant, but it can build up if vitinho isn't careful.

You're probably fine with this particular game, but if you want to try anything larger you should keep this in mind.

Here's where I got this information.

Quick quote:
Quote: "It turns out our bottleneck wasn't CPU cycles so much as it was cache misses. And because of our lovely, heavy object-oriented design, we had constant pointer dereferencing and traversing graphs all over memory.

We managed to gain some performance back by changing some lists to contiguous arrays and doing some prefetching, but overall, it was very difficult to meet the performance requirements we wanted for the game."


Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 30th Dec 2011 03:51
Yes. You have to be careful not to create memory leaks. The linked list method is benificial when you are making an indeterminant amount of objects. As I pointed out in my first post, you could make a HUGE array even if you only use a small portion of it.

The fastest code is the code never written.
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 30th Dec 2011 05:15
Quote: "As I pointed out in my first post, you could make a HUGE array even if you only use a small portion of it."

I saw that, just wanted to point out a potential consequence of the system you're proposing.

And IMHO Hawkblood's system would be fine for every type of 2D shooter I can think of except perhaps a bullet-hell shooter where you'd be traversing a truly gigantic list(I've seen upwards of 1,000 bullets in some of those games!) every single loop.

vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 30th Dec 2011 11:33
well.. How bad can that memory leaks be :S ?

@HaWKBlood

I just added the
BULLET *LastBullet,*FirstBullet,*BulletAccess;//this will replace the same code I had in my example as global
to the player struct, and deleted the global one.

I still dont know how :S to create the bullet as the mouse is clicked

But i understood your code and all..

Thanks

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 31st Dec 2011 15:01
1000 bullets won't give you trouble unless you have a very slow machine. I suspect most people on this forum have at least a moderately fast computer.

Memory leaks will only occur if you loose a pointer. If you keep the pointer controls within your structure (which is where they are), then there will be no problems.

To create a bullet, you could use some code like this:



The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 01:46 Edited at: 1st Jan 2012 01:49
Sorry for the stupidity :S

HAPPY NEW YEAR first of all

Here's the player struct:




You forgot to mention that some errors happen taking off the global pointers but i fixed them.

I got an error when i try to make a bullet:

Unhandled exception at 0x010339bf in Shoot2Survive4.exe: 0xC0000005: Access violation writing location 0x00000000.

At this line:


What to do? thanks for the help

@edit:
Sorry if i didnt mention but here it is the shooting function, its pretty the same as yours

void Player::Shoot(int mx, int my, float S)
{
float D=dbATANFULL((x-mx), (y-my));
LastBullet->make(x+(width/2), y+(height/2), D, S); //the divided by 2 width and height is so it can be more at the middle.
}

then in the Update player function:



C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 02:31 Edited at: 1st Jan 2012 02:32
I think I know what is happening. You are not initializing the variables. You will get that if it's NULL. Notice the location is 0x00000000 ? That's the NULL value.

Do this outside of your game loop (after you declare your player variable):


Remember my first example? I had to ititialize them before I could use them.

Also, are you deleting the list when you exit your game? You MUST do this or you WILL get memory leaks.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 02:47 Edited at: 1st Jan 2012 02:50
How to delete it? xD
:S can it damage my pc?

@edit:
I just tested the intialization and no error, but no bullet either..

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 03:17
Show me the code that displays the bullet.


to delete them
Make a void DestroyPlayer(); function in your Player struct and put this in there:



The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 14:28
Well i used it on the Player Update Function:


Its wrong right?

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 15:44
That looks right. Did you run into any problems?

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 16:00
No bullet at the screen..

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 16:36
Show me Shoot(..) function

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 17:45
Is just like yours:



C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 19:46
Sorry. I meant the function that moves the bullets.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 20:03
Ah, its pretty much yours i think:

All Bullet Codes:


I checked and for me... i think nothing is wrong but im not so good with this lists and stuff..

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 20:19
That should work. You need to make the bullet's speed very small (less than 1). I would make it 0.01 for now. I think the bullet is going off-screen so fast you can't see it. When you have the bullets visible, then change the speed to what looks good.

There may be another problem. Let me see your game loop (I need to see where your dbSync() is).

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 21:45


I tested the speed 0.01 and same thing > nothing :S

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 21:47
Give me your classes. I'll test it on my machine to see what the problem is.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 21:50
i use structs

Here's the all source code:



C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 22:14
This looks so cool when you do a stream of them. Change this:

And in your Player::update()

Notice the last line? You had "LastBullet->MoveBullets();"

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 22:43 Edited at: 1st Jan 2012 22:49
No shoot and i replaced all you said.. :S

Why the hell it works with you and not with me?

@EDIT it works now
It was because i was clearing the screen to white and the bullets are white.. xDD

But for now the angle is wrong.. :S how to fix it? (Its not pointing bullets to the correct location)

And the bullets are not spawning in the player direction...

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 1st Jan 2012 22:56
I am hunting down another problem with destroying the bullets. I will post the code in its entirety when I figure it out.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 1st Jan 2012 22:57
Ok thanks, and sorry for the disturb :S

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Jan 2012 01:30
Sorry it took so long. I was hunting down problems with destroying the bullets. Here is the complete code (I had to make my own player sprite):

I know you are going to have questions, so here is an attempt to answer some ahead of time.
-I had to get rid of the BULLETS::movebullets() function. I think there's a problem with destroying a pointer while you are using it...? So I made a new function Player::MoveBullets(). It handles it beautifully.
-I put a "count" variable in there to keep track of creation and destruction. When all the bullets are off the screen, this should be 0.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 2nd Jan 2012 11:50 Edited at: 2nd Jan 2012 12:44
And... it works perfectly!!!!

And you were right its a cool effect when u keep pressing the mouse

Now i want to know what name you want to be presented at the Credits

@edit;
Do you have an idea how can i make a collision system for the bullets?

Im thinking using the Bullet.x and Bullet.y and check if they are equal to enemy x and y or higher than the height and width of the enemy... it seems easy but if you have another way

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Jan 2012 14:17
Don't worry about the credit. This is something I will use in my next game. And it was a good exercise to test out a theory.

Give me a basic sketch of how the game will play. Is it a side-scroller, is it top down....?

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 2nd Jan 2012 15:39
its top down
and im thinking making some kind of scroll so the map is bigger than the window size, if you know how to this too please tell me because for this i have no idea..

And.. thank you for the help and time you "wasted" (for you might be wasted but for me wasn't) with me

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Jan 2012 16:34
If the screen will move, then you need to make ALL your coordinates absolute (not to the screen). This means that the coordinates will have to reference the top-left of the screen to be able to render to the screen properly. It's not hard. I assume your character will be center screen all the time? This would be the easiest to do, since the top-left of the screen would be an exact distance from the character at all time. You would move your character as normal- keeping track of his location. When you go to render (update) you would send all the objects the character's location minus the top-left reference (if char centered then subtract half the width and half the height of the screen).

Quote: ""wasted""
Oh, it's not wasted. As I said, I hadn't done this before and I wanted to test it out.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 2nd Jan 2012 16:48
Well...
i made this of the screen scrolling back in allegro and now i will try to implement it to DGDK but later. Now the real thing i want to know is if it was you making this game

what method to detect collision between enemies and bullets do u use?

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Jan 2012 18:39
Have a size for your enemies (x&y) and check the bullet's location each time you update and if it's within the enemies location+/- size, then *HIT*

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 2nd Jan 2012 19:03
Ah its like my method then
Thanks a lot

But only one thing:
I put that if(hit) in the MoveBullets function right?

Im thinking put it this way:



C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Jan 2012 20:15
This needs some work:

That code will do nothing unless the bullet is EXACTLY at the enemy's location. You need some *error* to deal with. How about this:


The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 2nd Jan 2012 21:12
and the location for the code?
its cool that way?

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Jan 2012 21:26

The enemy.x,enemy.y I assume you have and the enemy.sizeX and enemy.sizeY I think you will have to put in your enemy struct. I would initialize them to the enemy sprite's diminsions.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 2nd Jan 2012 21:39
Yes thanks when i arrive home i will test it

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 8th Jan 2012 15:15
You never told me if you got it to work.

The fastest code is the code never written.
vitinho444
14
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 8th Jan 2012 16:47
oh srry it worked very fine

C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)

Login to post a reply

Server time is: 2024-11-19 04:31:52
Your offset time is: 2024-11-19 04:31:52