TGB/MiniTutorials/ObjectMouseEvents
From TDN
| Mouse Input for Scene Objects | |
|---|---|
Description: This sample script will demonstrate how to set your scene window and scene objects to enable and handle mouse event callbacks.
To test the resource download the zip file and extract it to your ../tgb/Resources folder. Start up a new project in TGB and add the ObjectMouseEventExample resource to your project (Ctrl+R). Drag and drop a couple GG logos into your level and run it. You should be able to drag them and throw them with the mouse pointer. |
|
|
function BounceBall::onLevelLoaded(%this, %scenegraph)
{
// make sure the scene window is set to send mouse events to objects
if(!sceneWindow2D.getUseObjectMouseEvents())
{
sceneWindow2D.setUseObjectMouseEvents(true);
}
// set this object to accept mouse events
%this.setUseMouseEvents(true);
}
function BounceBall::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
// reset linear and angular velocity
%this.setLinearVelocity("0 0");
%this.setAngularVelocity(0);
// set the collision response to CLAMP and make it immovable temporarily
%this.setCollisionResponse("CLAMP");
%this.setImmovable(true);
// make this dragable
sceneWindow2D.draggingBall = %this;
// store temp time/pos and mouse offset
%this.tmpTime = getSimTime();
%this.tmpPos = %this.getPosition();
%this.mouseOffset = t2dVectorSub(%this.tmpPos, %worldPosition);
}
function sceneWindow2D::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
if(isObject(%this.draggingBall))
{
// reset linear and angular velocity
%this.draggingBall.setLinearVelocity("0 0");
%this.draggingBall.setAngularVelocity(0);
// get tmp pos and update last pos
%this.draggingBall.lastPos = %this.draggingBall.tmpPos;
%this.draggingBall.tmpPos = %this.draggingBall.getPosition();
// get tmp time and update last time
%this.draggingBall.lastTime = %this.draggingBall.tmpTime;
%this.draggingBall.tmpTime = getSimTime();
// move draggingBall to mouse pos + offset
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
}
}
function sceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
if(isObject(%this.draggingBall))
{
// update current time and get time difference from last time
%currTime = getSimTime();
%deltTime = %currTime - %this.draggingBall.lastTime;
// get curent position and vector from last position
%currPos = %this.draggingBall.getPosition();
%dirVec = t2dVectorSub(%currPos,%this.draggingBall.lastPos);
%forceVec = t2dVectorScale(%dirVec, 300/%deltTime);
// set the collision response back
%this.draggingBall.setCollisionResponse("RIGID");
%this.draggingBall.setImmovable(false);
// set the linear velocity of the ball
%this.draggingBall.setLinearVelocity(%forceVec);
// reset the dragging ball variable
%this.draggingBall = "";
}
}
// Bouncing Ball datablock
datablock t2dSceneObjectDatablock(BounceBallDatablock)
{
class = "BounceBall";
CollisionActiveSend = "1";
CollisionActiveReceive = "1";
CollisionPhysicsSend = "1";
CollisionPhysicsReceive = "1";
CollisionCircleSuperscribed = "0";
CollisionDetectionMode = "CIRCLE";
CollisionResponseMode = "RIGID";
Damping = "0.5";
GraphGroup = "1";
WorldLimitCallback = "0";
WorldLimitMax = "50 37.5";
WorldLimitMin = "-50 -37.5";
WorldLimitMode = "BOUNCE";
};
|



