Re: Programming question

yes, I forget to add the object position :

unsigned int width = 0;
unsigned int height = 0;
system->getScreenSize(&width, &height);

float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");

// ray
MVector3 origin = camera->getTransformedPosition();
MVector3 dest = camera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));

// bounding box
MBox3d * bbox = text->getBoundingBox();
MVector3 pos = text->getTransformedPosition();

if(isEdgeToBoxCollision(origin, dest, pos + bbox->min, pos + bbox->max))
    // intersection

or (slower but more correct) :

unsigned int width = 0;
unsigned int height = 0;
system->getScreenSize(&width, &height);

float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");

// ray
MVector3 origin = camera->getTransformedPosition();
MVector3 dest = camera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));

// bounding box
MBox3d * bbox = text->getBoundingBox();

// local ray
MVector3 localOrigin = text->getInversePosition(origin);
MVector3 localDest = text->getInversePosition(dest);

if(isEdgeToBoxCollision(localOrigin, localDest, bbox->min, bbox->max))
    // intersection

27

Re: Programming question

First doesn't work but second works perfectly
I've found solution to my problem with empty scene:  i used mvariable in behavior with empty blank (such as "Camera Name"), and you mustn't use such names (good name is "cameraName" for example). May be it will be useful for somebody

28

Re: Programming question

So, workable version of text behavior(to create simple menu)
MouseClick.h

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MouseClick.h
// 
// Code : Anael Seghezzi & hog
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _MOUSE_CLICK_H
#define _MOUSE_CLICK_H

#include <MEngine.h>


class MouseClick : public MBehavior
{
public:

    // constructors / destructors
    MouseClick(MObject3d * parentObject);
    MouseClick(MouseClick & behavior, MObject3d * parentObject);
    ~MouseClick(void);

private:
    
    // custom variables

    const char* m_cameraName;
    const char* m_sceneName;
    MVector4 m_colour;
    const char* m_soundName;
    const char* m_functionName;
    bool m_play;
    
public:

    // destroy
    void destroy(void);

    // get new
    static MBehavior * getNew(MObject3d * parentObject);

    // get copy
    MBehavior * getCopy(MObject3d * parentObject);

    // name
    static const char * getStaticName(void){ return "MouseClick"; }
    const char * getName(void){ return getStaticName(); }

    // variables
    unsigned int getVariablesNumber(void);
    MVariable getVariable(unsigned int id);

    // events (virtuals from MBehavior class)
    void update(void);
    //void onBeginScene(void);
    void runEvent(int param){}
};

#endif

MouseClick.cpp

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MouseClick.cpp
// 
// Code : Anael Seghezzi & hog
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "MouseClick.h"


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init, this part is always similar, constructor, copy constructor etc
/////////////////////////////////////////////////////////////////////////////////////////////////////////

// constructor
MouseClick::MouseClick(MObject3d * parentObject):
MBehavior(parentObject),
m_cameraName(""),
m_sceneName(""),
m_colour(1,1,1,1),
m_soundName(""),
m_functionName(""),
m_play(true)
{}

// copy constructor
MouseClick::MouseClick(MouseClick & behavior, MObject3d * parentObject):
MBehavior(parentObject),
m_cameraName(behavior.m_cameraName),
m_sceneName(behavior.m_sceneName),
m_colour(behavior.m_colour),
m_soundName(behavior.m_soundName),
m_functionName(behavior.m_functionName),
m_play(behavior.m_play)
{}

// destructor
MouseClick::~MouseClick(void)
{}

// destroy function : always similar
void MouseClick::destroy(void)
{
    delete this;
}

// getNew function : always similar
MBehavior * MouseClick::getNew(MObject3d * parentObject)
{
    return new MouseClick(parentObject);
}

// getCopy function : always similar
MBehavior * MouseClick::getCopy(MObject3d * parentObject)
{
    return new MouseClick(*this, parentObject);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables, allow to access custom variable from script and from Maratis Editor
/////////////////////////////////////////////////////////////////////////////////////////////////////////

unsigned int MouseClick::getVariablesNumber(void){
    return 5;
}

MVariable MouseClick::getVariable(unsigned int id)
{
    switch(id)
    {
    default:
        return MVariable("NULL", NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable("cameraName", &m_cameraName, M_VARIABLE_STRING);
    case 1:
    return MVariable("sceneName", &m_sceneName, M_VARIABLE_STRING);
    case 2:
        return MVariable("colour", &m_colour, M_VARIABLE_VEC4);
    case 3:
    return MVariable("soundName", &m_soundName, M_VARIABLE_STRING);
    case 4:
    return MVariable("functionName", &m_functionName, M_VARIABLE_STRING);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
/////////////////////////////////////////////////////////////////////////////////////////////////////////










// update function (called by MGame class by default)
void MouseClick::update(void)
{
MEngine * engine = MEngine::getInstance();//engine
MGame * game = engine->getGame();//game
MSystemContext * system = engine->getSystemContext();
MInputContext * input=engine->getInputContext();

if( !game->isRunning())
        return;

    MObject3d * parent = getParentObject();
    MLevel * SampleLevel=engine->getLevel();//level
MScene *    SampleScene = SampleLevel->getSceneByName(m_sceneName);//scene
MOText*    SampleText = SampleScene->getTextByName(parent->getName());//text
MOCamera*    SampleCamera=SampleScene->getCameraByName(m_cameraName);//camera
    MOSound * SampleSound=NULL;
    SampleSound=SampleScene->getSoundByName(m_soundName);
    MScriptContext * SampleScript=engine->getScriptContext();
MBox3d*    bbox = SampleText->getBoundingBox();
unsigned int width=0;
unsigned int height=0;
    system->getScreenSize(&width, &height);
    
float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");
MVector3 origin = SampleCamera->getTransformedPosition();
MVector3 dest = SampleCamera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));
MVector3 localOrigin = SampleText->getInversePosition(origin);
MVector3 localDest = SampleText->getInversePosition(dest);

    if(isEdgeToBoxCollision(localOrigin, localDest,bbox->min,bbox->max)){        
        SampleText->setColor(m_colour);    
        if(SampleSound)
            if(m_play){
            SampleSound->play();
            m_play=false;
            }
            if(input->onKeyDown("MOUSE_BUTTON1"))
            //if(SampleScript)
            SampleScript->callFunction(m_functionName);
    }
    else
    {
        SampleText->setColor(MVector4(0.8,0.8,0.8,0.6));
    m_play=true;
    
    }
    
}

Behavior properties:
1)cameraName - camera that captures text
2) sceneName - scene, where texts/sounds take place
3)colour - text highlighting(r,y,b,a), when you put mouse on it
4)soundName - sound, when you put mouse on text
5)functionName - the function that would be called, when we press on text (must be in you scene script)
example:

...
function funcName()
...
end

you can change the default text colour by editing

SampleText->setColor(MVector4(0.8,0.8,0.8,0.6));

video
http://youtu.be/_l2oAbihUX4

Last edited by hog (2014-06-04 08:02:23)

Re: Programming question

seems nice,

about the script,
it will be faster if you call a function from the current scene script so you are not loading/compiling the script each time :

replace m_scriptName with m_functionName and use "callFunction" instead :

if(input->onKeyDown("MOUSE_BUTTON1"))
    SampleScript->callFunction(m_functionName);

30

Re: Programming question

wow i didn't think about that,
in this way it willn't change native scene's script
p.s updated link to video
p.p.s updated code with anael's advice

Last edited by hog (2014-06-04 08:01:24)

Re: Programming question

I can see the video now, promising !!

32

Re: Programming question

Hi,
i need to move object and i tried to do it with

MPhysicsContext * Phys=engine->getPhysicsContext();
MObject3d * parent = getParentObject();
Phys->addCentralForce(objId?, MVector3(x,y,0));

how can i get objectId?

Re: Programming question

Hi !
here you are :

MOEntity * entity = (MOEntity *)parent; // be sure that parent is an entity
MPhysicsProperties * phyProps = entity->getPhysicsProperties();
unsigned int objId = phyProps->getCollisionObjectId();