Game Development Community

Draging the Tilemap or World

by Sean Doherty · in Torque Game Builder · 03/09/2007 (9:14 am) · 14 replies

I'm trying to drag the tilemap, world, or camera by clicking the left mouse button and moving the mouse. It seems to work; except the coordinates passwed are world coordinates. So basically, the world jumps when I let go of the mouse, reposition, and attempt to drag it further. Is there a may to get relative mouse coordinates or something else I can do?

Here is some sample code:

function sceneWindow2D::onMouseDragged( %this, %modifier, %worldPos, %mouseClicks )
{
 	mountainLayer.setPositionX(%worldPos* -1);  

}

Thanks

#1
03/09/2007 (10:13 am)
Can you elaborate on what you're trying to do? There are easy ways to transform coordinates between spaces (like world to object, etc), but it sounds like you might just be looking to use the vector functions (which are all there in Torquescript - check the docs->TGB Reference) to find the drag offset so that the object will be dropped where you really released it.
#2
03/09/2007 (2:19 pm)
Tom is correct in needing more info. The more detailed you can be in your question the more helpful the community can be.

One thing I notice right off. In your setposition you are doing a %worldPos* -1.
%worldPos is a vector type. i.e '10 10', when you multiply that by a value TorqueScript will not error but assume the first value in the vector and return a number instead of a vector.

if you want to multiply a vector you can split the vector into its x and y components and multiple each value separately or use t2dVectorMult.

remember Documentation is your friend 8)
#3
03/09/2007 (2:38 pm)
Or scale it by -1 instead of multiply it.
#4
03/09/2007 (11:10 pm)
I'll probably want the vector function at some point, but right now I'm only working with the x coordinate. Basically, I want to be able to move a tilemap left or right using by dragging it with the mouse button down. I want reposition the tilemap (tilelayer) with SetPositon or SetPanPosition (I think).

However, when I grab the tilemap and set the positon using the worldposition everything works fine; except when I release the tilemap and repositon the mouse for the second attempt it jumps because the world position is off by the amount I move the mouse while not being dragged.

This is difficult to explain. Basically, I'm trying to implement a system where you can repositon the ground, in a game, by grabbing it and pulling it left or right. Once the mouse hits the edge of the screen you have to reposition your mouse a drag it again to continue the scrolling.

I think if I had the relative mouse position within the onMouseDragged command everything would be fine?

Hope this helps.
#5
03/12/2007 (2:37 pm)
Anyone have any additional questions?
#6
03/12/2007 (2:46 pm)
No more questions from me, but I guess I'll answer yours ;)

Basically, you want to use t2dVectorSub to find the offset from the cursor's worldPos to the center of the object (use getPosition for that). Do that when the mouse is clicked on the object. Then, when the mouse is dragged, reposition the object to the mousePosition added to the saved offset. That way, you'll move the part of object you're actually clicking on.

Hope that made sense - I could write it out in script if it didn't :)
#7
03/14/2007 (4:26 pm)
Thanks, I got is working! Here is the code: (I had some trouble finding the getWord function)

function sceneWindow2D::onMouseDown( %this, %modifier, %worldPos, %mouseClicks )
{
	echo("----Mouse Down----");

	%vecMaintainPosition = mountainLayer.getPosition();
	%vecMousePosition = %worldPos;
	%vecOffset = t2dVectorSub(%vecMaintainPosition, %vecMousePosition);

	//Set to global.
	$vecOffset = %vecOffset;
}


function sceneWindow2D::onMouseDragged( %this, %modifier, %worldPos, %mouseClicks )
{
	echo("----Mouse Dragged----");

	%vecNewPosition = $vecOffset + %worldPos;
 	mountainLayer.setPositionX(getWord(%vecNewPosition,0));  
}
#8
03/14/2007 (8:14 pm)
I altered my code to move the camera instead of the tilemap, but it seems to be jumping around all over the place?

function sceneWindow2D::onMouseDown( %this, %modifier, %worldPos, %mouseClicks )
{
	echo("----Mouse Down----");

	%vecMaintainPosition = %this.getCurrentCameraPosition();
	%vecMousePosition = %worldPos;
	%vecOffset = t2dVectorSub(%vecMaintainPosition, %vecMousePosition);

	//Set to global.
	$vecOffset = %vecOffset;

	echo($vecOffset);
}


function sceneWindow2D::onMouseDragged( %this, %modifier, %worldPos, %mouseClicks )
{
	echo("----Mouse Dragged----");

	%vecNewPosition = t2dVectorSub($vecOffset, %worldPos);

	%this.setCurrentCameraArea(getWord(%vecNewPosition,0), getWord(%vecNewPosition,1), (getWord(%vecNewPosition,0)+100), (getWord(%vecNewPosition,1)+75));
}
#9
03/14/2007 (9:57 pm)
You could use the camera's "move to" method, which should smoothly move the camera around, rather then 'jump' the camera ...
#10
03/15/2007 (3:02 pm)
Actually, I started with the following; but it was even worse (there must be a logic issue):

%this.startCameraMove(1.5);
%this.completeCameraMove();
#11
03/15/2007 (11:14 pm)
Anyone have any ideas?
#12
03/16/2007 (1:29 pm)
With the code you show, your camera will still jump to the location.
When you execute the completeCameraMove() that jumps the camere to the location rather than scrolling it over time.

The way to scroll with the camera is to use:
//assuming %this is your current scenewindow. 
%this.setTargetCameraPosition(x, y, width, height); //This set the target position where we want the
                                                                                    // camera to move to
//Now call our startmove to move the camera to the new position
%this.startCameraMove(%time);    //startCameraMove %time is the time in seconds it will take to move to
                                                        //the new position

I would recommend looking at the documentation for TGB and experimenting. In my opinion the best way to learn is to play with the various functions and their parameters and see what effect they have. This approach has worked quite well for me.
#13
03/17/2007 (3:29 pm)
I was actually using these (sorry posted wrong code):

%this.setTargetCameraPosition(getWord(%vecNewPosition,0), getWord(%vecNewPosition,1), 100, 75);
%this.startCameraMove(1.5);

It scrolls smoth, but didn't seem to move correctly. What's wrong with using setCurrentCameraArea?

PS: I've been reading the documentation; trust me:)
#14
03/18/2007 (10:42 am)
Hmm, this is harder than one would think. Because the camera moves, it causes the mouse coordinates to change altering the numbers:

Point A	Point B	Point C	Point D	Point E
Offset	45	45	45	45	45
Mouse	-12	-1	12	25	-82

New Camera	-57	-46	-33	-20	-127

Point E is the second drag; after the screen has finished scrolling.