Projectile System Collison

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.
Post Reply
bochek
Posts: 12
Joined: Mon Dec 19, 2005 7:20 pm

Projectile System Collison

Post by bochek »

I am having a little bit of trouble getting my collision system to work on my guns... here is some code it is very self explanitory..


This gets called by the draw loop.

Code: Select all

void checkCollision()
{

    
     if(Bullet)
     {
        if(Character)
        {
            if (characterIsAlive)
            {
                if(Character->getTransformedBoundingBox().intersectsWithBox(Bullet->getTransformedBoundingBox()))
                {
                   Character->setVisible(false);                                                                                          
                   characterIsAlive = false;
             
                }
            }
        }
     }
}
and this is the function that gets called when you shoot

Code: Select all


void shoot(core::vector3df position, core::vector3df rotation, core::vector3df endPosition)
{  
   if(intCount >0)
   {
        BulletMesh = smgr->getMesh("media/Weapons/BFG/bullet/bullet.3ds");
        Bullet = smgr->addAnimatedMeshSceneNode(BulletMesh);
        
        core::vector3df addRotation;
        addRotation = core::vector3df(90,0,0);
        
        core::vector3df end = (endPosition - position);
        end.normalize();
        end = position + (end * camera->getFarValue());
        
    
        
        Bullet ->setScale(core::vector3df(20,20,20));
        Bullet ->setPosition(position);
        Bullet ->setRotation(rotation + addRotation);
        Bullet->setMaterialFlag(video::EMF_LIGHTING, false);
        
        f32 length = (f32)(end - position).getLength();
        const f32 speed = 10.0f;
        u32 time = (u32)(length / speed);
        
        anim = smgr->createFlyStraightAnimator(position, end, time);
        Bullet->addAnimator(anim);	
        anim->drop();
        
        anim = smgr->createDeleteAnimator(time+5);
        Bullet->addAnimator(anim);
        anim->drop();

        
        for(int x = 0; x < 10; x++)
        {
            
            anim = smgr ->createCollisionResponseAnimator(ObjectSelector[x], Bullet, core::vector3df(1,1,1),
            core::vector3df(0,0,0),
            core::vector3df(0,50,0));
            Bullet->addAnimator(anim);
            anim -> drop();
        }
        
            anim = smgr ->createCollisionResponseAnimator(mapSelector, Bullet, core::vector3df(1,1,1),
            core::vector3df(0,0,0),
            core::vector3df(0,0,0));
            Bullet->addAnimator(anim);
            anim -> drop();
            

           
    }
    
}
It only crashes if the first shot misses the character.

Any help would be appreciated

Thanks,
Bochek
Guest

Post by Guest »

BUMP^^

anyone.... school projects due tomorow! need help quick please!

Bochek
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

Does the delete animator delete the object? If so, then your variable Bullet still points to that location in memory, where the Bullet no longer exists if the delete animator clears it. This is whats causing the crash. When the bullet hits, you are probably deleting it and setting its reference to NULL, like you should, but you need to take into account what happens when the delete animator clears the bullet.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

How funny i had the exact same problem with my fps game...actually my code looked alot like yours.....anyways i droped the project cause i couldn't find the error...than i started the bomberman game and its almost done there is just this little network problem....Anyways what i wanted to say is that i learned a better way of doing it.

class CBullet
{
protected:
ISceneNodeAnimator* anim;
ISceneNode* node;
IAnimatedMesh* mesh;
vector3df startposition;

int type;
int range;
int speed;
bool alive;
ISceneManager* smgr;

public:
bool getLife()
{
return alive;
}
CBullet()
{
alive = true;
smgr = device->getSceneManager();
}
CBullet(int mtype, vector3df position, vector3df orientation,int mrange, int mspeed)
{
type = mtype;
range = mrange;
speed = mspeed;
alive = true;
startposition = position;
smgr = device->getSceneManager();

if(mesh = smgr->getMesh("YOUR MODEL"))
{
node = smgr->addAnimatedMeshSceneNode(mesh);
}
if(node)
{
node->setPosition(position);
node->setRotation(orientation);

anim = smgr ->createCollisionResponseAnimator(metaselector, node, core::vector3df(1,1,1),
core::vector3df(0,0,0),
core::vector3df(0,50,0));
node->addAnimator(anim);
anim -> drop();
}
}
void kill()
{
//Don't how the animator is called correctly
if(node)
{
anim = smgr->createDeleteAnimator(1);
node->addAnimator(anim);
anim->drop();
}
alive = false;
}
void render(std::vector *players)
{
node->setPosition(node->getAbsolutePosition()+orientation*speed);
if(node->getAbsolutePosition().getDistanceFrom(startposition) == range || node->getAbsolutePosition().getDistanceFrom(startposition) > range)
{
kill();
}else
{
if(players)
{
for(int i;i<players->size();i++)
{
if(players->Character->getTransformedBoundingBox().intersectsWithBox(node->getTransformedBoundingBox()))
{
switch(type)
{
case 1:
//do something with character
break;
case 2:
//do something with character
break;
case 3:
//do something with character
break;
}
}
}
kill();
}
}
}
};

//needed variables
std::vector<CBullet> bullets;
std::vector<YOUR PLAYERCALSS> players;

int i;
vector<CBullet>::iterator biter;

//in your while loop
while(i<bullets.size()+1)
{
if(bullets.getLife())
{
bullets.render(players);
}
else
{
bullets.erase(biter);
}
biter++;
i++;
}

//when u shoot a bullet !exampel"
bullets.push_back(CBullet(1,vector3df(30,10,6),vector3df(0,90,0),300,50);

I don't know if it works i just made this class up but i hope it does and helps u.
Just a tip only use it for slow and visible objects like rockets. for actual bullets u should use a raycast.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply