Game Development Community

Setting Up Controls A Moving Box.

by rennie moffat · in Torque Game Engine · 07/24/2009 (7:57 am) · 14 replies

Hi I want my player to move to a slecected position. This position is chosen by a single tap or mouse down event to select the X and Y. So far I believe I cam going to build a PlayerControl.cs game file. I have determined it to be as such... or hold these things. Now I just have to set it, can some please explain the basic mechanics of scripting this. I am reading along, but finding it hard to follow, when I need to create my onw.



///controls playersMovement including mouse event triggers



function onMouseDown(%this, %worldPos, %mouseClicks)
{
   ///this must set a world position, where the Player must move to.
   %this.setPosition(x,y);
   return;
}


///move player to touched point (not sure if this should be inside the mouse down event
%this.setMousePosition($pShip.getPosition(x,y)); 
%this.owner.moveTo(%this.worldPos,);
 
 
///Set Player Movement Style.
 Acceleration
 Linear Velocity
 Dampening
 getPosition
 MoveTo
 Animation for each movement(4LinearDirections)

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
07/24/2009 (8:11 am)
ok, so I am just working on it, but a few things have me confused. Mostly, what to put into an entire game script when it appears some variables like Acceleration and Dampening, might be better added on as a separate behaviour. Am I right?


this is my code thus far.. any thing good?


function onMouseDown(%this, %worldPos, %mouseClicks)
{
   ///this must set a world position, where the Player must move to.
   %this.setPosition(x,y);
   %this.setMousePosition($pShip.getPosition(x,y)); 
   %this.owner.moveTo(%this.worldPos, %speed);
   return;
}


ps.moveTo includes speed. Lets just say this worked. Where would speed be determined (ie. 30mph)
#2
07/24/2009 (11:48 am)
Setting the position in TorqueScript will skip the interpolation of the position (and rotation).
And you will get a very jerky move.

You need some sort of setMoveDestination() for players.
You can see how it is done in the aiPlayer class.

Another decision to move to a certain point is to create an interpolation between the two points and set the new render transform.
This way you will get a smooth move.
#3
07/24/2009 (11:52 am)
interpolation?
#4
07/24/2009 (11:56 am)
The point3F class already has an interpolation interface.

Point3F player_pos = getPosition();
Point3F end_pos = your_mouse_position;

player_pos.interpolate(end_pos,player_pos,your_interpolation_step);
setRenderTransform(player_new_transform);
#5
07/24/2009 (11:56 am)
im thinking that an interpolation sets an object on a path for movement?


if so, my game, a maze game would require a path for every possible movement, possibly hundreds. Convenient?
#6
07/24/2009 (11:59 am)
No,i am talking about a simple linear interpolation between two positions.
#7
07/24/2009 (12:01 pm)
hmm ok,
i think i need to go back into the cave to really see this.
#8
07/24/2009 (12:04 pm)
As an example you can take a look at the player conform resource.
It uses the same idea for smooth tilt/roll the player.

I see this can be a bit difficult to understand for a non-programmer.
#9
07/24/2009 (12:09 pm)
a this point,
im sure it will improve.



steps must be taken.

oh vell thanks for the help tho, i will keep it rollin.




#10
07/24/2009 (1:49 pm)
function SceneWindow2D::onMouseDown(%this, %worldPos, %mouseClicks)
{
echo(%worldPosition);
$Player.moveTo(%worldPosition);
}


hi this code is basic, as I want my click's worldPosition to be recognized and then the have the player move toward that spot. IS this way off?

I am thinking more code must follow, to determine player movement style (velocity etc.)


:?
#11
07/25/2009 (12:41 pm)
function PlayerMovementBehavior::onMouseDown(%this, %worldPosition)

{
setMousePosition(%worldPosition);

 
}



so I have this to set the mouse position on mouse down, I want the player to then move toward this point. I am sure this is very easy, but I am finding it difficult to nail down the code.
#12
07/25/2009 (1:23 pm)
function sceneWindow2D::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks){
		
		setPosition(%worldPosition);
	}
	
	function sceneWindow2D::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks){
		movePlayer(%worldPosition);
	}
something that I have pulled up.


workable i think?
#13
07/25/2009 (1:40 pm)
I have tried this code, but added to a behaviour. Is something as simple as this possible, the right track for what I am trying to do? It seems like it should be so simple to do.
#14
07/25/2009 (3:10 pm)
ok so here is this code, i found, however, I am not sure how to link it to a mouse behaviour/event.


function t2dSceneObject::setDestination(%this, %dest)
{
        if((%dest != 0) && (%dest !$= ""))
        {
                %this.Destination = %dest;
                %this.isMoving = true;
                if($debugMsg::pathDebug)
                        echo("calling move to dest with" SPC %dest);
                %this.schedule(50,moveToDest);
        }
}