Keeping track of bullet particles before/after collision
Keeping track of bullet particles before/after collision
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
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
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.
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.
-
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
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.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.
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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;
Then just to test that my idea will work ;
Great that worked 
so I tried this
DOH!! segmentation fault at runtime 
but of course this works
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
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[];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"));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"));but of course this works
Code: Select all
enemy[0].node = smgr->addCubeSceneNode(10.0f,0,-1,core::vector3df (10,10,10));I thought that it might be the addCubeSceneNode=> 'id'
so I changed it for the second instance of bullet, no joy
any ideas?
Thanks
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.
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.
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.
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.
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
Or use a dynamically sizing array?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.
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
OK, I think I need some more help if you wouldn't mind pointing me in the right direction.
I have my structure defined:
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;
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 ;
So I thought that I would 'wrap' my DansGame class in a namespace;
then in DansPhysics
Now I am getting ;
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.
I have my structure defined:
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.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];
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]);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 declaredSo I thought that I would 'wrap' my DansGame class in a namespace;
Code: Select all
namespace Dans_video_game { ... };Code: Select all
using namespace Dans_video_game;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 declaredIf 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.
I forgot to mention that i have
and
in my headers respectively
Code: Select all
#ifndef INC_DAN_PHYSICS_H
#define INC_DAN_PHYSICS_H
Code: Select all
#ifndef INC_DAN_GAME_H
#define INC_DAN_GAME_H-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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.
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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

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:
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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.
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.
