Keeping track of bullet particles before/after collision

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Keeping track of bullet particles before/after collision

Post by marrabld »

Hi,

I am after some advice; I have set up a simple shoot--(a single)bullet--collide with something and destroy it ... errrr "game".

My problem is, how do I keep track of an arbitrary number of bullets. Is there a standard way of doing this. At the moment I just have an up date position and check for a collision against map, then against my only other "character"node. This approach obviously wont work if I want a user controlled number of bullets.

This is what I was thinking (and about to try out)

Every time I call my shoot() method, it creates a "bullet"node, with a position is space (obviously), and a corresponding array that holds a unit vector in the direction of travel, mass etc .

The plan then is to save that array and node into a data structure. In my drawing loop I have a routine that checks all of the fields in the data structure (all my bullets in the map) for collisions against all fields in a similar "enemy"structure and updates any the new positions.

Problems I foresee ::>

I'm not sure how to update the structure so that when my bullet nodes are dropped the ones that are left are shuffled down to the starting field numbers. I don't want it to get infinitely large as I shoot more and more.

mmm, I am sure that there will be other problems.

I am happy to go off and start this approach and find the results I am just hoping that if there is a simpler or standard procedure someone might point it out.

Thanks in advance
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I use std::vector quite a lot for these sorts of things. I don't know if it's the most efficient but it works. A linked list could be a good approach too as you can delete arbitrary items very easily and quickly.

So basically make a class for your bullet and in that class have a pointer to the bullet node, the direction it's travelling in etc. Then have a vector or list to which all these bullets are added then each frame iterate through them all (using an iterator, duh!) and update them, check for collisions and delete them as necessary.
Image Image Image
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

Great!, good advice, Ill update this as I go.

Cheers
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: Keeping track of bullet particles before/after collision

Post by rogerborg »

marrabld wrote:I'm not sure how to update the structure so that when my bullet nodes are dropped the ones that are left are shuffled down to the starting field numbers. I don't want it to get infinitely large as I shoot more and more.
You shouldn't have to shuffle them all down. Unless there's some particular reason why they need to be kept in order, when a bullet expires you can just delete it, then move the last bullet down to replace it.

Remember to process the newly moved-down bullet immediately rather than skipping it, e.g. if you're looping through an array using an incrementing counter, decrement it so that the same array index is processed again next time round the loop.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

Thanks rogerborg, yes of course you are right oops.


OK this is what I have done (for a proof of concept);

I have defined a structure;

Code: Select all

struct PointMass
	{
            scene::ISceneNode* node;
            float mass;
            core::vector3df velocity;
            core::vector3df acceleration;
            core::vector3df force;

	} bullet[], enemy[];
Then just to test that my idea will work ;

Code: Select all

bullet[0].node = smgr->addCubeSceneNode(10.0f,0,-1,core::vector3df (10,10,10));
bullet[0].node->setMaterialTexture(0, driver->getTexture("./media/t351sml.jpg"));
Great that worked :)

so I tried this

Code: Select all

bullet[1].node = smgr->addCubeSceneNode(10.0f,0,-1,core::vector3df (10,10,10));
bullet[1].node->setMaterialTexture(0, driver->getTexture("./media/t351sml.jpg"));
DOH!! segmentation fault at runtime :(

but of course this works

Code: Select all

enemy[0].node = smgr->addCubeSceneNode(10.0f,0,-1,core::vector3df (10,10,10));
can some one tell me why bullet[1].'stuff' is invalid?

I thought that it might be the addCubeSceneNode=> 'id'

so I changed it for the second instance of bullet, no joy

any ideas?

Thanks
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

What size do you imagine your bullet and enemy arrays to be?

You're not actually defined their size so i guess they've defaulted to just allocate enough memory for one PointMass struct. Put a number in between the square braces and check that you don't try to go above that number when accessing the arrays.
Image Image Image
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

Thanks, yes that worked.

i was under the impression that leaving them blank made them dynamic in size, like strings. O well, I will have to write an error trap that stops me from dynamically creating more bullets than I allow for in my class.

Cheers.

If I get it all working, Ill update the details in case someone else is curious.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I hope that by strings you don't mean char arrays as they behave exactly the same! :lol:

They can't be dynamic as C/C++ doesn't provide memory management for you in that way!
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

marrabld wrote:O well, I will have to write an error trap that stops me from dynamically creating more bullets than I allow for in my class.
Or use a dynamically sizing array?

std::vector<> and irr::core::array<> are both resizable.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

OK, I think I need some more help if you wouldn't mind pointing me in the right direction.

I have my structure defined:
struct PointMass
{
scene::ISceneNode* node;
float mass;
core::vector3df velocity;
core::vector3df acceleration;
core::vector3df force;
//more fields to follow soon.

} bullet[50], enemy[5];
And I figured I would pass them to an update() method in the drawing loop that would scan the fields and update the positions, detect collision etc inside that.

I then got thinking that I would create my own V.Simple physics library to do this in another class and file that I could add more mehthods to as I felt like it.

So I have created a new file dan_physics.h and dan_physics.cpp. in the header I defined a class called 'class DansPhysics' and in dan_physics.cpp ---> void DansPhysics::update(struct DansGame::PointMass bullet[50] ,struct DansGame::PointMass enemy[5]);


I then instantiated it 'DansPhysics physics;' in my main.cpp, thinking that I could;

Code: Select all

physics.update(bullet[50],enemy[5]);
in my DansGame::run(); method

This didn't work because the two classes couldn't see each other (and possibly other reasons). I got things in DansPhysics like ;

Code: Select all

dan_physics.h:20: error: ‘DansGame’ has not been declared

So I thought that I would 'wrap' my DansGame class in a namespace;

Code: Select all

namespace Dans_video_game { ... };
then in DansPhysics

Code: Select all

using namespace Dans_video_game;
Now I am getting ;

Code: Select all

game.cpp: In member function ‘void Dans_video_game::DansGame::run()’:
game.cpp:299: error: no matching function for call to ‘DansPhysics::DansPhysics(Dans_video_game::DansGame::PointMass [50], Dans_video_game::DansGame::PointMass [5])’
dan_physics.h:12: note: candidates are: DansPhysics::DansPhysics()
dan_physics.h:12: note:                 DansPhysics::DansPhysics(const DansPhysics&)
dan_physics.h:8: error: ‘Dans_video_game’ is not a namespace-name
dan_physics.h:8: error: expected namespace-name before ‘;’ token
dan_physics.h:20: error: ‘DansGame’ has not been declared
dan_physics.h:20: error: ‘DansGame’ has not been declared
I don't understand why I am being told its not a valid name space?

If I put the different classes in the same name space, shouldn't they be able to see each other and each others members?

I am obviously doing something wrong but im not sure what it is.
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

I forgot to mention that i have

Code: Select all

#ifndef INC_DAN_PHYSICS_H
#define INC_DAN_PHYSICS_H
and

Code: Select all

#ifndef INC_DAN_GAME_H
#define INC_DAN_GAME_H
in my headers respectively
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Urgh, it's Hump Day, I can't follow that.

Any chance you could just upload your project somewhere? I'm sure we can solve it in seconds if we can see the entire code.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

I'm a bit embarrassed, I should have checked my code more thoroughly before I posted.

I maid a typo in my #include"DanPhysics.h" DOH!

It compiles OK now.

I am still having trouble passing my structures but I am going to give that more of a go before I ask for any help

Sorry for posting cryptic rants

;)
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

It's OK, I'm really enjoying the passion. I'd just like to be in a position to offer more effective assistance.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

Great I just about have it all working now! I pass the structure and I can get it to update the position based on the velocity field YAY!.

Am I in the right company if I admit I am enjoying myself ;)

Thanks for the assistance.

When I get it all tidied up I may post a quick "how to" and offer other people to comment on better or more efficient ways.
Post Reply