Topic: problems with ray using c ++

I wanna work with maratis SDK following the example of the engine manual use, I'm trying to make a ray to know on top of which object is the mouse placed regarding what IsRayHit() function does which always returns false instead of detecting the object, I leave you the code         

                float x = input->getAxis("MOUSE_X");
                float y = input->getAxis("MOUSE_Y");

                MVector3 vi = camera->getUnProjectedPoint(MVector3(x,y,0));
                MVector3 vo = camera->getUnProjectedPoint(MVector3(x,y,1));

                //cout<<" "<<vo.x<<" "<<vo.y<<" "<<vo.z<<endl;

                if(physics->isRayHit(vi, vo, &collisionObjId, &point)){
                    exit(0);
                }
                //cout<<point.x<<"  "<<point.y<<"   "<<point.z<<endl;

                MOEntity * entity = scene->getEntityByName("monkey");
                MPhysicsProperties * phyProps = entity->getPhysicsProperties();

                cout<<phyProps->getCollisionObjectId()<<endl;

The object has a physical property active, but the ray is not detecting it

Last edited by ArielRiv (2015-03-26 16:43:36)

Re: problems with ray using c ++

Hi,

getUnProjectedPoint is low level, you need to give exact pixel position in viewport space.
If you are in full screen, by multiplying by the screen size + reversing y coordinates :

MSystemContext * system = engine->getSystemContext();
    
unsigned int width = 0;
unsigned int height = 0;
system->getScreenSize(&width, &height);

x = x * width;
y = (1.0f - y) * height;

In lua it's done transparently.

Re: problems with ray using c ++

Thanks for your help Anael. I have other question: why doesn´t work the object’s physical simulation when I active their physical properties after the scene is loaded in the Update () function?

Re: problems with ray using c ++

collision objects are created during MScene::begin automatically,
to add collision objects later on, you need to do it manually.

Normally you can call this to create the collision object of an entity if it was not done at scene begin (call it only once, it is not checking if it was already done) :

scene->prepareCollisionShape(entity);
scene->prepareCollisionObject(entity);
scene->prepareConstraints(entity);