Game Development Community

Binding mouse to an object

by Paul Z · in Torque Game Builder · 03/25/2005 (11:48 am) · 6 replies

Folks,
How can I bind mouse movements to an object on the screen, like a paddle in Breakout? I've looked at the previous discussions on binding mouse, but that's not what I am looking for.
Also, if it is possible to bind my object to the mouse/cursor, is there any way to control the speed (both X and Y) of the movements within the screen? Here is what I tried, and it works as intended, but not what I need...
new ActionMap(playerMap);
cursorOff();
playerMap.bind( mouse0, "zaxis",MZ );   
playerMap.bind( mouse0, "yaxis",MY );   
playerMap.bind( mouse0, "xaxis", MX );
playerMap.push();
}

function MZ (%val)
{
echo( "MouseZ" SPC %val );
}

function MY (%val)
{
echo( "MouseY" SPC %val );
}

function MX (%val)
{
echo( "MouseX" SPC %val );
}

TIA, Paul Z.

#1
03/25/2005 (11:53 am)
Just reading through quickly... so correct me if I' thinking down the wrong road...

recently I just made it so I could select an object with a mouse and it would then be "bound" to the mouse until I let it go...

there are a couple ways to do this, some easy, some trickier... is this the behavior your trying to acheive ? the object being attached to where the mouse is ?
#2
03/25/2005 (12:29 pm)
Check out

fxSceneWindow2D.setUseMouse

and

fxSceneWindow2D::onMouseMove

I'm using them to track the last position of the mouse, find the difference with the new position and move my chosen object accordingly.
#3
03/26/2005 (6:10 am)
@Matt,
Yes, I need my object to be where my mouse is, and as I move the mouse, the object should move in the same direction. It is irrelevant if I use the mouse buttons (and I will), their use does not affect the movement of the object. Overall, what I want to implement is a ping pong type game where the player moves one of the paddles using the mouse.

@Joseph,
I looked at it briefly, but could not figure out the syntax... The Reference Guide specifies MouseStatus? as a function parameter. But there is no example of how itis to be used. I tried several ways like "setUseMouse=true", etc. but it does not work. I am probably issing some basic understanding how it works, and therefore cannot put it together. If you have a piece of code that you are willing to share I would very much appreciate it.
It is unlikely I will need onMouseMove, but I will certainly be using right/left buttons.

Thanks, Paul Z
#4
03/26/2005 (6:35 am)
@Paul

For fxSceneWindow2D.setUseMouse, just replace fxSceneWindow2D with the actual name of your scene window. For example, in the tutorial, it is called sceneWindow2D, so to implement mouse use:

sceneWindow2D.setUseMouse(true);

And I believe, although I could be wrong about this, you need to override the default implementation of onMouseMove() with your own version, like so:

function fxSceneWindow2D::onMouseMove(%this, %modifier, %worldPosition, %mouseClicks) {
     // add your mouse movement code in here
     echo("mouse moved" SPC %worldPosition);
}

This is similar to using the onCollisionCallback function.

Hope this helps,
Chris
#5
03/26/2005 (4:06 pm)
Paul - here's most of the script I'm using to do my inventory. When a person clicks on an object it is "bound" to the mouse until they click again.

function hudWindow2D::OnMouseDown(%obj, %mod, %pos, %clicks)
{ 	
	//Did we click on something in the HUD?
	%found = hudSceneGraph.pickPoint(%pos,$hudGroup,$hudLayer);
	
	if (getWordCount(%found) > 0)
	{
		%inv = $inventory.getTileLayer(0);
		%found = firstWord(%found);
		
		//Clicked on the Inventory window
		if (%found == %inv)
		{			
			%tileNum = %inv.pickTile(%pos);
			else // clicked on an object
			{			
				$holdingObject = removeItem(%tileNum);
				$holdingCell = %tileNum;
				if (isObject($holdingObject)) $mouseOrigPos = %pos;
			}
		}

	}

	
}

Then in my mouse move I have:
function hudWindow2D::OnMouseMove(%obj,%mod,%pos,%clicks)
{
	if (isObject($holdingObject))
	{
		%oPos = $holdingObject.getPosition();
		%ox = getWord(%oPos,0);
		%oy = getWord(%oPos,1);
		%mx = getWord($mouseOrigPos,0);
		%my = getWord($mouseOrigPos,1);
		%mx -= getWord(%pos,0);
		%my -= getWord(%pos,1);
		%ox -= %mx;
		%oy -= %my;
		
		$holdingObject.setPosition(%ox SPC %oy);
		$mouseOrigPos = %pos;
	}

}

I tried to clear out some of the clutter, so that exact code may not work.
It also doesn't handle releasing an object from the mouse.

Just make sure you have
sceneWindow2D.setUseMouse(true);
somewhere in your code and those functions in a file that's been exec'd and you should be good to go.
#6
03/27/2005 (6:13 am)
Chris, Joseph,
Thank you guys very much for you help!!! With the snippets you provided I think I can get it working... All I need to figure out now is how to bind the mouse automatically to a specific object at start of the game. And that should be easy. I will post the result as soon as I get it to work...
Again, thank you very much!!!
Paul Z.