Topic: Use of lua in maratis3d iOS project for hit testing

I make a 3D gallery app.
In that app I have three meshes on the  first "screen".
Each mesh represents category of 3D models to view.
When user tap on one of these meshes, camera should translate to next screen with several meshes.
Can I use lua for mesh hit testing (ie determine to what the mesh user touched)?

Can I send (trigger) events from lua to my Objective C iOS code?
For example, when user select one of the categories and  camera translates to it, I need to change UI (buttons, labels) in app.

Last edited by Petr (2012-09-05 09:23:51)

Re: Use of lua in maratis3d iOS project for hit testing

Look through here for all the functions:

http://wiki.maratis3d.org/index.php?tit … tions_list

All the functions you need for this are in there.
It's just a matter of taking the time to actually do some research.

Re: Use of lua in maratis3d iOS project for hit testing

You can communicate from lua to c++ (and so for objective c I suppose)

For example by creating your own script function :

int exampleFunc(void)
{
    MEngine * engine = MEngine::getInstance();
    MScriptContext * script = engine->getScriptContext();
    
    if(script->getArgsNumber() == 2)
    {
        int firstArgument = script->getInteger(0);
        int secondArgument = script->getInteger(1);
        
        // do a simple addition
        script->pushInteger(firstArgument+secondArgument);
        return 1; 
    }
    
    return 0;
}



MyGame * game = NULL;

void StartPlugin(void)
{
    // get engine
    MEngine * engine = MEngine::getInstance();

    // add script function
    MScriptContext * script = engine->getScriptContext();
    script->addFunction("testFunction", exampleFunc);
    
    // set my game
    game = new MyGame();
    engine->setGame(game);
}

void EndPlugin(void)
{
    SAFE_DELETE(game);
}

There is also examples to download here : http://www.maratis3d.org/?page_id=53

Re: Use of lua in maratis3d iOS project for hit testing

Ok, thank you.

Re: Use of lua in maratis3d iOS project for hit testing

I cannot find method for hit testing in lua maratis3d docs. I found method RayHit, but I cannot understand fhow can I use it to determine on which mesh touch occurs.
SDK has functionality for this?

Re: Use of lua in maratis3d iOS project for hit testing

There is multiple way to do it, you can use RayHit by sending a ray starting from the touch position,
or you can use collision detection :

- move a cube (collision physics as a ghost) depending on the touch position.
- detect if there is a collision with other objects (also set as ghost) : http://wiki.maratis3d.org/index.php?tit … ionBetween

Re: Use of lua in maratis3d iOS project for hit testing

here is the function i created to pick objects :

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), (1 - 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;
}

Usage :

obj = pickObject(Camera, x, y)
if obj then
...
end

Last edited by Mikey (2012-09-25 16:56:13)

Re: Use of lua in maratis3d iOS project for hit testing

Thank you for sharing!

Re: Use of lua in maratis3d iOS project for hit testing

Petr wrote:

Thank you for sharing!

there was i bug, i updated post
P.S.

changed 
        MVector3 rayD = camera->getUnProjectedPoint(MVector3(x * float(width), y * float(height), 1));
to 
        MVector3 rayD = camera->getUnProjectedPoint(MVector3(x * float(width), (1 - y) * float(height), 1));