Game Development Community

Google Earth style dragging

by Andrea Farid Marsili · in iTorque 2D · 04/10/2011 (7:59 am) · 13 replies

Hi, as you maybe know I'm converting my game from GameSalad. I'm a lot of stuff but I have some problem with my "Google Earth" style dragging.
if(actor.touch.pressed){
	actor.dragging = true;
	actor.dragOriginX = scene.Camera.Origin.X;
	actor.dragStartX = game.Touches.Touch1.X;
}

if(actor.dragging){
	constrain:
		scene.Camera.Origin.X
			TO
		min(526, max(0, actor.dragStartX - game.Touches.Touch1.X) + actor.DragOriginX);
}

if(actor.touch.released){
	actor.dragging = false;
}

PS:
minUsage: min(x,y)
Return the smaller value of two numbers or variable values.
Example: min(12,35) = 25

maxUsage: max(x,y)
Return the higher value of two numbers or variable values.

Constrain behavior:
This behavior continuously updates the value of one attribute to that of another. For instance, an actor's x and y position can continuously be constrained to that of the mouse.

Here an example of what i want to do (I'm doing it only for the X axis):
http://gamesalad.com/game/2380

And here my TorqueScript code:

function ActorName::onMouseDown(%this, %modifier, %worldPosition, %clicks){
	ActorName.dragStartX = getWord(%worldPosition, 0);
	ActorName.dragOriginX = getWord(sceneWindow2D.getCurrentCameraPosition(), 0);
	
	if(ActorName.dragging){
		return;
	}
	else{
		ActorName.dragging = true;   
	}
}

function oniPhoneTouchUp(%touchCount, %touchX, %touchY){
   ActorName.dragging = false;
}

function oniPhoneTouchMove(%touchCount, %touchX, %touchY){
	if(ActorName.dragging){
		%cameraX = t2dGetMin(526, t2dGetMax(0, ActorName.dragStartX - %touchX) + ActorName.dragOriginX);
		// if min() and max() would exist also in torqueScript:
		// min(526, max(0, ActorName.dragStartX - %touchX) + ActorName.dragOriginX);
		sceneWindow2D.setCurrentCameraPosition(%cameraX, 0)
	}
}

Obviously my code doesn't work because I don't know how to use the setter and the getter for the camera with iTorque2D and I don't know if they work also with iTorque2D (I have only some TGB experience).

#1
04/10/2011 (10:43 am)
The camera is essentially your t2dSceneWindow (by default named sceneWindow2d).

so it would be:

sceneWindow2D.setCurrentCameraPosition(%cameraX, 0, %width, %height);

Hope this helps.

#2
04/10/2011 (12:43 pm)
Thank you. I modified the code in the main post.
The end is near, I only need to know how to replace the min() and max() function.
#3
04/10/2011 (1:36 pm)
mClamp( number, min, max);
#4
04/11/2011 (1:47 pm)
Cannot understand one thing the "number" argument...what is it?
#5
04/11/2011 (6:11 pm)
Sorry better options:

t2dGetMin(526, t2dGetMax(0, ActorName.dragStartX - %touchX) + ActorName.dragOriginX);
#6
04/11/2011 (11:35 pm)
Where did you find these function?
I've got a TorqueScript reference pdf but a lot of function is missing...
I'll try using t2dGetMin and t2dGetMax and letmyou know if they work.
Thank you again.
#7
04/19/2011 (5:53 am)
Tried it and it doesn't work. I cannot figure why. Same logic work like a charm on GameSalad.

I found the problem thanks to some echo function.

function ActorName::onMouseDown(%this, %modifier, %worldPosition, %clicks){  
    ActorName.dragStartX = getWord(%worldPosition, 0);  //working
    ActorName.dragOriginX = getWord(sceneWindow2D.getCurrentCameraPosition(), 0);  //working
      
    if(ActorName.dragging){  
        return;  
    }  
    else{  
        ActorName.dragging = true;     
    }
    
    echo("DragStartX: " @ ActorName.dragStartX);
    echo("DragOriginX: " @ ActorName.dragOriginX);
    echo("CameraPositionOnClick: " @ sceneWindow2D.getCurrentCameraPosition());
}

function oniPhoneTouchUp(%touchCount, %touchX, %touchY){  
   ActorName.dragging = false;
}

function oniPhoneTouchMove(%touchCount, %touchX, %touchY){
    %xPosition = getWord(%touchX, 0); //working
    if(ActorName.dragging){
        %cameraX = t2dGetMin(526, t2dGetMax(0, ActorName.dragStartX - %xPosition) + ActorName.dragOriginX);
        sceneWindow2D.setCurrentCameraPosition(%cameraX, 0); //working (tried to set the camera to 10, 0)
    }  
    echo("xPosition: " @ %xPosition);
    echo("CameraX: " @ %cameraX);
}

%cameraX is always 0, I think that's the problem is the t2dGetMin and t2dGetMax.
#8
04/19/2011 (8:42 am)
Hmmmmmmmmmmmmm

Not related to your issue but you should change:
if(ActorName.dragging){    
        return;    
    }    
    else{    
        ActorName.dragging = true;       
    }

to
ActorName.dragging = true;

It's generally faster just to redundantly set a single value rather than checking it first.

Going back to your issue at hand...

I'll have to give this a test, let me go and boot up the old mac!
#9
04/19/2011 (9:34 am)
OK this works for me (not very smooth mind you) but only in one direction.

The reason is that of course moving in the opposite direction will always be smaller than 0 as it's a minus number.

Perhaps in GameSalad the starting location was something like 261 with 0 being the left side limit and 526 the right side limit?

Either way the 0 should represent your camera movement limit to the left and the 526 your camera movement limit to the right, in Torque by default your camera starts at 0,0.
#10
04/19/2011 (12:37 pm)
How did you manage to get it working? Did you made some code change?
GameSalad camera works as iTGB one, I think.
The 526 was only the half size of my pic.
#11
04/19/2011 (12:58 pm)
Made no notable changes, my callbacks are a little different as I've been fiddling around with mutli-touch but that shouldn't have mattered
#12
04/19/2011 (9:41 pm)
So you made no change...why it doesn't work for me?
Could you post the code?
#13
04/28/2011 (12:58 pm)
I solved my problem creating two button and setting the "backgroud" speed pressing the button.