Topic: Collision and camera in plugins question?

Hi, im making a plugin in c++ and want entity to colide with level mesh and stop moving when it collided. What functions i should use? I'm using isObjectInCollision, but it doesnt behave as i expected(just rotates with mouse movement). Actually i cant find description for this function, so i'm not sure what it does. How to find that collision occured?  Please help. Currently i have:

void Player::move(MObject3d * parent,MScene * scene,MEngine * engine,float speed)
{
        MVector3 pos=parent->getTransformedPosition();
        MVector3 rot=parent->getTransformedRotation();
        MOEntity* ent= scene->getEntityByName("Player");
        MPhysicsContext * physCont = engine->getPhysicsContext();
        MPhysicsProperties * phyProps = ent->getPhysicsProperties();

        pos.x += sin(rot.y)*speed;
        pos.y += cos(rot.y)*speed;
        pos.z += sin(rot.y)*speed;

        if (physCont->isObjectInCollision(phyProps->getCollisionObjectId())>0)
        {
            parent->setPosition(pos);
        }
}

Last edited by Skywriter (2013-09-19 16:50:01)

Re: Collision and camera in plugins question?

Hi,

you sure it's not :

if (physCont->isObjectInCollision(phyProps->getCollisionObjectId()) == 0) // 0 collisions so move
{
    parent->setPosition(pos);
}

how is the collision shape of your entity, and is the physics enabled in the entity and in the level ?

Also let's say you are using the entity bounding box,
once you collide with the level and cannot move it means the entity will be stuck because it cannot move when in collision.

Also, when dealing with physics objects, it's better to move them with physCont->addCentralForce instead of setPosition.
isObjectInCollision returns the number of collision the entity is with.

Can you detail how exactly you want the entity to interact ?

Re: Collision and camera in plugins question?

Hi, Anael. I have physics enabled in editor, but not in code. Actually have no idea how to do this in code, so if you have time for this, please post it for me. What i'm trying to achieve is to stop entity from moving when it collides with something(anything in level like wall or other entity) and of course bactrack or slide when collision occurs. I tried

if (physCont->isObjectInCollision(phyProps->getCollisionObjectId()) == 0) // 0 collisions so move
{
    parent->setPosition(pos);
}

but without sucess. Object still moves threw the wall. I tried to add

phyProps->setCollisionShape(M_COLLISION_SHAPE_BOX);

but still nothing. What i'm missing?

Last edited by Skywriter (2013-09-18 21:18:37)

Re: Collision and camera in plugins question?

if the physics of your entity and level is enabled in editor you don't need to enable it in code.

do you want to replicate something like the jules movement or sponza in c++ ? with gravity ?
if you want the entity to bounce/slide you don't need to stop the entity manually, let the physics handle it.

also, who do you want to move, "parent" or "ent" ?

first, enable physics in editor (be sure entity has a mass > 0)
then move the entity with physCont->addCentralForce only :

        MPhysicsContext * physCont = engine->getPhysicsContext();
        MVector3 rot = parent->getTransformedRotation();
        MPhysicsProperties * phyProps = parent->getPhysicsProperties(); // use parent physics prop if you want to move the parent

        MVector3 dir;
        dir.x = sin(rot.y)*speed;
        dir.y = cos(rot.y)*speed;
        dir.z = sin(rot.y)*speed;

        physCont->addCentralForce(phyProps->getCollisionObjectId(), dir);

Re: Collision and camera in plugins question?

Thanks Anael. Collision worked. I used box for collision shape, and sponza for level. I have modified function slightly. So far i have:

void Test::move(float speed)
{
        MEngine * engine = MEngine::getInstance();
        MLevel * level = engine->getLevel();
        MScene * scene = level->getCurrentScene();
        MObject3d * parent = getParentObject();
        MOEntity * entity = (MOEntity *)parent;
       
        MPhysicsContext * physCont = engine->getPhysicsContext();
        MVector3 rot = entity->getTransformedRotation();
        MPhysicsProperties * phyProps = entity->getPhysicsProperties(); // use parent physics prop if you want to move the parent

        MVector3 dir;
        dir.x = sin(rot.y)*speed;
        dir.y = cos(rot.y)*speed;
        dir.z = sin(rot.y)*speed;

        physCont->addCentralForce(phyProps->getCollisionObjectId(), dir);
}

Last edited by Skywriter (2013-09-19 11:59:03)

Re: Collision and camera in plugins question?

I applied camera to object, but when i press "W" or "S" key camera moves independently from object. Why this is happening? Any sugestions, this is my code:

void Test::move(float speed)
{
        MEngine * engine = MEngine::getInstance();
        MLevel * level = engine->getLevel();
        MScene * scene = level->getCurrentScene();
        MObject3d * parent = getParentObject();
        MOEntity * entity = (MOEntity *)parent;
        MOCamera *camera = scene->getCurrentCamera();
        MInputContext * input = engine->getInputContext();
        MPhysicsContext * physCont = engine->getPhysicsContext();
        MPhysicsProperties * phyProps = entity->getPhysicsProperties();

        MVector3 rot = entity->getTransformedRotation();

        camera->setPosition(parent->getTransformedPosition());
        
        if(input->isKeyPressed("W"))
        {
            MVector3 dir;
            dir.x = sin(rot.y)*speed;
            dir.y = cos(rot.y)*speed;
            dir.z = sin(rot.y)*speed;
            
            
            physCont->addCentralForce(phyProps->getCollisionObjectId(), dir);

        }
        if(input->isKeyPressed("S"))
        {
            MVector3 dir;
            dir.x = sin(rot.y)*(-speed);
            dir.y = cos(rot.y)*(-speed);
            dir.z = sin(rot.y)*(-speed);
            
            
            physCont->addCentralForce(phyProps->getCollisionObjectId(), dir);
        }
        
        if(input->isKeyPressed("A"))
        {
            MVector3 dir,rotation;
            dir=parent->getEulerRotation();

            parent->setAxisAngleRotation(MVector3(0,0,1),dir.z+1);
        }

}

Last edited by Skywriter (2013-09-19 17:09:09)

Re: Collision and camera in plugins question?

I also thought about this, it would be great if my camera wouldn't go through objects or house-walls when I came near.

Re: Collision and camera in plugins question?

For a camera, you can replicate what is done in Jules demo in lua but in c++,
basically apply physics to an invisible box (with sphere collision shape) with very low mass (0.001 for example) that follow your player or else, and link the camera to this box so the camera will collide walls.

Re: Collision and camera in plugins question?

So I can replicate what you did in Jules demo if I want to make it through lua?

Last edited by Pär (2013-09-20 14:31:56)

Re: Collision and camera in plugins question?

You can inspire what is done in lua to do the same in c++,
follow what I said and remember to always use physCont->addCentralForce to move objects with physics

Re: Collision and camera in plugins question?

I dont know much about c++ to be honest, but when I need that plugin for my game I will make it.