Topic: Physics

Is there a way to display physic bodies ?
Does fuctions set rotation and else transform bodies ?

Re: Physics

There is no physics display in editor,
but you can use the box.mesh, cylinder.mesh, cone.mesh and sphere.mesh of "Small Demos" example
to build a collision body, set it invisible and then attach an object to it.

The rotation/scale etc transform the bodies yes.

Re: Physics

Is there a way to display physic bodies ?

> But we'll add that on the TODO list

Re: Physics

Thanks for fast response, when i tried to pick objects (with function i wrote in my plugin) i faced the problem that after rotation the body is not rotated, so

The rotation/scale etc transform the bodies yes.

so, i think i have a problem with my function below big_smile
can you look at this function and say whats wrong ? (I use only perspective projection)

int pickObject(void)
{
    MEngine * engine = MEngine::getInstance();
    MScriptContext * script = engine->getScriptContext();
    
    if (script->getArgsNumber() == 3)
    {
        MOCamera * camera = (MOCamera *)script->getPointer(0);
        float x = script->getFloat(1);
        float y = script->getFloat(2);

        MPhysicsContext * physics = engine->getPhysicsContext();
        
        unsigned int width, height;
        engine->getSystemContext()->getScreenSize(&width, &height);

        MVector3 rayO = camera->getTransformedPosition();
        MVector3 rayD = camera->getUnProjectedPoint(MVector3(x * float(width), y * float(height), 1));
        rayD = rayO + ((rayD - rayO).getNormalized() * (camera->getClippingFar() - camera->getClippingNear()));

        unsigned int objId;
        if (physics->isRayHit(rayO, rayD, &objId))
        {
            MLevel * level = engine->getLevel();
            MScene * scene = level->getCurrentScene();
            unsigned int entCount = scene->getEntitiesNumber();
            for (unsigned int i = 0; i < entCount; ++i)
            {
                MOEntity * entity = scene->getEntityByIndex(i);
                MPhysicsProperties * phyProps = entity->getPhysicsProperties();
                if (phyProps)
                {
                    if (phyProps->getCollisionObjectId() == objId)
                    {
                        script->pushPointer(entity);
                        return 1;
                    }
                }
            }
        }
    }
    
    return 0;
}

sometimes if works wrong (don't detect collision)

Re: Physics

I think the y mouse needs to be inverted :
Your camera is not in ortho mode ?

MVector3 cameraPos = camera->getTransformedPosition();
MVector3 mousePos3d = camera->getUnProjectedPoint(MVector3(x*width, height - y*height, 1));
    
MVector3 vec = (mousePos3d - cameraPos).getNormalized();
    
MVector3 rayO = pos + vec*camera->getClippingNear();
MVector3 rayD = pos + vec*camera->getClippingFar();

Re: Physics

anael wrote:

I think the y mouse needs to be inverted :
Your camera is not in ortho mode ?

MVector3 cameraPos = camera->getTransformedPosition();
MVector3 mousePos3d = camera->getUnProjectedPoint(MVector3(x*width, height - y*height, 1));
    
MVector3 vec = (mousePos3d - cameraPos).getNormalized();
    
MVector3 rayO = pos + vec*camera->getClippingNear();
MVector3 rayD = pos + vec*camera->getClippingFar();

Perspective. Thanks for hint, i missed about y axis smile