About Store Forum Documentation Contact



Post Reply 
Hit detection
Author Message
Harry Offline
Member

Post: #1
Hit detection
When I shot from my weapon I check if phys hit detect some object and do nothing if this object is a player (I use PhysHitCallback). If object is an item I check all objects near this item and if these objects are characters I want to add them damage. But when in item range is more than 1 character my application crashes.

Unhandled exception at 0x02acf326 in Tutorials.exe: 0xC0000005: Access violation reading location 0x00000000.

Simplify code:
Code:
Bool PlayerHitDetection2::hit(PhysHit &phys_hit)
{
    if(phys_hit.obj) // if the actor comes from an object
    {
        if(Player *player=CAST(Player,phys_hit.obj))
        {
        }else if(Item *item=CAST(Item,phys_hit.obj))
        {
                      Vec tempPos;
              tempPos=item->pos();
                      Memc<Game::Obj*> objs;
              Game::World.objGet(objs,Ball(6,tempPos));
              REPA(objs)
              {
                           if(AI *chrs=CAST(AI,objs[i]))
                   {    
                                chrs->addDamage(RandomF(4,8),&Players[0]);
                            }
            }
return true;
}
void Player::shoot()
{
            Vec     position, direction; ScreenToPosDir(Vec2(0),position,direction);
            PlayerHitDetection2 phd;
            Physics.ray(position,direction*D.viewRange(),phd); // there shows me error
}
(This post was last modified: 04-02-2010 07:58 PM by Harry.)
04-02-2010 03:17 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Panerox Offline
Member

Post: #2
RE: Hit detection
i have not used PhysHitCallback.
maybe the problem in a local variable PlayerHitDetection2 phd;?
Or return value PhysHitCallback:: hit...
(This post was last modified: 04-02-2010 04:14 PM by Panerox.)
04-02-2010 04:09 PM
Find all posts by this user Quote this message in a reply
Harry Offline
Member

Post: #3
RE: Hit detection
I tried with global and had the same.
04-02-2010 04:12 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: Hit detection
why is there no 'return' in the bool method?
04-02-2010 07:07 PM
Find all posts by this user Quote this message in a reply
Harry Offline
Member

Post: #5
RE: Hit detection
This return is in my code but I forget to put it when I copy this shorter version pfft I edited it.
(This post was last modified: 04-02-2010 07:58 PM by Harry.)
04-02-2010 07:57 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: Hit detection
this shouldnt be the problem, but you're not checking if
"),&Players[0]);"
0-th element exists

the rest looks ok, I don't know what could cause the problem

maybe the problem is somewhere else.

what's the full code for 'PlayerHitDetection2' do you have any other methods/constructors or just 'hit' ?
04-02-2010 08:12 PM
Find all posts by this user Quote this message in a reply
Harry Offline
Member

Post: #7
RE: Hit detection
Just hit:

Code:
struct PlayerHitDetection2 :PhysHitCallback
{     
    virtual Bool hit(PhysHit &phys_hit);
};
Bool PlayerHitDetection2::hit(PhysHit &phys_hit)
{
    Vec direction; direction=ScreenToDir(Vec2(0));
    if(phys_hit.obj)
    {
        if(Player *player=CAST(Player,phys_hit.obj))
        {
        }else if(Item *item=CAST(Item,phys_hit.obj))
        {
            item->addBulletHole(phys_hit.plane.pos, phys_hit.plane.normal, direction);
            if(item->type==ITEM_NONE)
                if(item->type2==ITEM_ACTIVE)
                {
                    explosion.create(main,single,0x00600000,32,phys_hit.plane.pos,3);

                    smokPart.reborn=true;
                    smokPart.set(item->matrixScaled());
                    smokPart.reset();
                    smokPart.reborn=false;

                    QuakeFx.addMedium();

                    // create destroyed bottle
                    Vec tempPos;
                    tempPos=item->pos();
                    Items.removeData(item);
                    Game::ObjParams &obj=*Game::Objs("Obj/Spacerniak/Butlades.obj"); // get barrel object parameters
                    Game::World.objCreate(obj,Matrix(obj.scale(),tempPos));    // create new object at (16,8,16) position and give objects default scaling

                    Memc<Game::Obj*> objs;
                    Game::World.objGet(objs,Ball(6,tempPos)); // 6 m range
                    REPA(objs)
                    {
                        if(Item *items=CAST(Item,objs[i]))
                        {
                            items->actor.addImpulse(Random.vec(1,3),tempPos);
                        }
                        if(AI *chrs=CAST(AI,objs[i]))
                        {
                            OrientP &hand=chrs->cskel.getPoint("HandL");
                            chrs->addDamage(RandomF(chrs->health*2.5/Dist(chrs->pos(),tempPos),chrs->health*2.5/Dist(chrs->pos(),tempPos)+10),&Players[0]);
                            chrs->ragdoll.vel(-hand.dir*10.0f+Random.vec(3,5));
                        }
                    }
                }
        }else if(Static *statics=CAST(Static,phys_hit.obj)) // if the object is a static
        {
            statics->addBulletHole(phys_hit.plane.pos, phys_hit.plane.normal, direction); // call item method to add a bullet hole
        }else if(Door *doors=CAST(Door,phys_hit.obj)) // if the object is an item
        {
            doors->addBulletHole(phys_hit.plane.pos, phys_hit.plane.normal, direction); // call item method to add a bullet hole
        }
    }else // the actor doesn't have an object set, so most probably it's a terrain actor
    {
        // add a marker to the world terrain
        Game::World.terrainAddDecal(WHITE,*Materials("Decal/bullet.mtrl"),Matrix().setPosDir(phys_hit.plane.pos, phys_hit.plane.normal).scaleOrn(0.05f));
    }
    return true;
}

This is all code.
(This post was last modified: 04-02-2010 08:37 PM by Harry.)
04-02-2010 08:36 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
RE: Hit detection
well this changes the situation, you're dynamically removing/creating actors inside physics callback function
perhaps PhysX doesn't allow that inside callback functions.

you should record information inside the callback
but perform them outside of the callback, this should help
04-02-2010 09:19 PM
Find all posts by this user Quote this message in a reply
Harry Offline
Member

Post: #9
RE: Hit detection
It doesn't help. But objects are removed and then created properly. Error is only when some characters is near destroying object. When I comment part which is responsible for add damage to this characters everything works fine. I also found that when in character die function I comment ragdollEnable(); error disappear. What could be wrong?
04-02-2010 10:04 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #10
RE: Hit detection
you should perform all operations outside the callback
inside just remember which objects you encounter
04-02-2010 10:27 PM
Find all posts by this user Quote this message in a reply
Harry Offline
Member

Post: #11
RE: Hit detection
Ok I moved everything outside the callback and now it works fine. Thank you for help smile
04-02-2010 11:06 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply