Mouse coordinates in game world
by Temasek Polytechnic · in Torque Game Engine · 05/24/2006 (6:41 pm) · 3 replies
I want to implement this:
The player can click anywhere in the game world and they will receive the x,y,z coordinates of that point in the game world.
I have read the Object Selection tutorial and implemented this:
gameTSCtrl.h
#ifndef _GAMETSCTRL_H_
#define _GAMETSCTRL_H_
#ifndef _DGL_H_
#include "dgl/dgl.h"
#endif
#ifndef _GAME_H_
#include "game/game.h"
#endif
#ifndef _GUITSCONTROL_H_
#include "gui/core/guiTSControl.h"
#endif
class ProjectileData;
class GameBase;
//----------------------------------------------------------------------------
class GameTSCtrl : public GuiTSCtrl
{
private:
typedef GuiTSCtrl Parent;
Point3F mMouseWPoint;
public:
GameTSCtrl();
bool processCameraQuery(CameraQuery *query);
void renderWorld(const RectI &updateRect);
void onMouseMove(const GuiEvent &evt);
void onRender(Point2I offset, const RectI &updateRect);
void onMouseDown(const GuiEvent &evt);
Point3F getMouseWPoint()
{
return mMouseWPoint;
}
DECLARE_CONOBJECT(GameTSCtrl);
};
#endif
gameTSCtrl.cc
//other functions were remove to save character space
void GameTSCtrl::onMouseDown(const GuiEvent &evt)
{
MatrixF mat;
Point3F vel;
if(GameGetCameraTransform(&mat,&vel))
{
//Point3F pos; //get camera position
//mat.getColumn(3,&pos);
Point3F screenPoint(evt.mousePoint.x,evt.mousePoint.y,-1);
Point3F worldPoint;
if(unproject(screenPoint,&worldPoint))
{
mMouseWPoint = worldPoint;
Con::executef(this,1,"onMouseDown");
}
}
}
ConsoleFunction( snapToggle, void, 1, 1, "()" )
{
gSnapLine = !gSnapLine;
}
ConsoleMethod(GameTSCtrl,getMouseWPoint,const char*,2,2,"()")
{
char* retBuffer = Con::getReturnBuffer(256);
const Point3F &point = object->getMouseWPoint();
dSprintf(retBuffer,256,"%g %g %g",point.x,point.y,point.z);
return retBuffer;
}
playGui.cs
//onWake and onSleep removed to save character space
function PlayGui::onMouseDown(%this)
{
error("====== OnMouseDown ======");
%mousePoint = %this.getMouseWPoint();
if($objChoice $= "Flamingo")
{
addFlamingo(%mousePoint);
}
else if($objChoice $= "Elephant")
{
}
}
function addFlamingo(%point)
{
%bot = AIPlayer::create("Flamingo" @ $nameCountF,%point,FlamingoNew);
StatusInfo.setText("You Have Just Added A Flamingo");
$nameCountF++;
return %bot;
}
Somehow, when i left-click the mouse, it does not call the onMouseDown function because the "====== OnMouseDown =====" was not printed out.
What went wrong?
The player can click anywhere in the game world and they will receive the x,y,z coordinates of that point in the game world.
I have read the Object Selection tutorial and implemented this:
gameTSCtrl.h
#ifndef _GAMETSCTRL_H_
#define _GAMETSCTRL_H_
#ifndef _DGL_H_
#include "dgl/dgl.h"
#endif
#ifndef _GAME_H_
#include "game/game.h"
#endif
#ifndef _GUITSCONTROL_H_
#include "gui/core/guiTSControl.h"
#endif
class ProjectileData;
class GameBase;
//----------------------------------------------------------------------------
class GameTSCtrl : public GuiTSCtrl
{
private:
typedef GuiTSCtrl Parent;
Point3F mMouseWPoint;
public:
GameTSCtrl();
bool processCameraQuery(CameraQuery *query);
void renderWorld(const RectI &updateRect);
void onMouseMove(const GuiEvent &evt);
void onRender(Point2I offset, const RectI &updateRect);
void onMouseDown(const GuiEvent &evt);
Point3F getMouseWPoint()
{
return mMouseWPoint;
}
DECLARE_CONOBJECT(GameTSCtrl);
};
#endif
gameTSCtrl.cc
//other functions were remove to save character space
void GameTSCtrl::onMouseDown(const GuiEvent &evt)
{
MatrixF mat;
Point3F vel;
if(GameGetCameraTransform(&mat,&vel))
{
//Point3F pos; //get camera position
//mat.getColumn(3,&pos);
Point3F screenPoint(evt.mousePoint.x,evt.mousePoint.y,-1);
Point3F worldPoint;
if(unproject(screenPoint,&worldPoint))
{
mMouseWPoint = worldPoint;
Con::executef(this,1,"onMouseDown");
}
}
}
ConsoleFunction( snapToggle, void, 1, 1, "()" )
{
gSnapLine = !gSnapLine;
}
ConsoleMethod(GameTSCtrl,getMouseWPoint,const char*,2,2,"()")
{
char* retBuffer = Con::getReturnBuffer(256);
const Point3F &point = object->getMouseWPoint();
dSprintf(retBuffer,256,"%g %g %g",point.x,point.y,point.z);
return retBuffer;
}
playGui.cs
//onWake and onSleep removed to save character space
function PlayGui::onMouseDown(%this)
{
error("====== OnMouseDown ======");
%mousePoint = %this.getMouseWPoint();
if($objChoice $= "Flamingo")
{
addFlamingo(%mousePoint);
}
else if($objChoice $= "Elephant")
{
}
}
function addFlamingo(%point)
{
%bot = AIPlayer::create("Flamingo" @ $nameCountF,%point,FlamingoNew);
StatusInfo.setText("You Have Just Added A Flamingo");
$nameCountF++;
return %bot;
}
Somehow, when i left-click the mouse, it does not call the onMouseDown function because the "====== OnMouseDown =====" was not printed out.
What went wrong?
About the author
#2
05/24/2006 (6:56 pm)
Hmm..well i added a right click function that will display the mouse cursor, along with a menubar(that means when they rightclick, the cursor and the menubar will appear)...will the menubar affect the onMouseOver?
#3
05/24/2006 (11:41 pm)
Can someone help me? I'm really stuck...when i press the mouse(button0), nothing happens..the "====== OnMouseDown =====" was not printed out. There is only a menu bar and a small GuiBitmapCtrl on the screen. So the mouseDown should still work right? Pls help..
Torque 3D Owner Jesse Liles