Game Development Community

A little bit of n00b help?

by Brandon Fenty · in Torque 2D Beginner · 03/28/2014 (8:23 pm) · 7 replies

I've worked my way through the tutorials, but still feel woefully inadequate in my understanding. I'm not new to programming, but I'm not quite getting the torque engine's quirks. For instance, I was trying to do collision in a top-down RPG. What I wanted to do (in meta code just to get the point across):

onCollision()
{
setposition(%oldposition);
}

move()
{
%oldposition = getPosition();
movement()
}

essentially, on collision, it puts the object back where it was the last frame before it collided. However, this is putting my object back at it's STARTING location in the world (0 0).

does this logic not work in torque2d? for movement I'm modifying the shooterControls.cs file, if that helps. Is there another resource with some more tutorials I can follow?

#1
03/28/2014 (8:57 pm)
The main issue I see with your pseudo code lies in the use of the % prefix.

For a detailed tutorial on TorqueScript, make sure to read this wiki article.

==

%variable => Local variable, only accessible from within the function where it is defined.

$variable => Global variable, accessible from anywhere in the code.

In your example, %oldposition has not been initialized in the onCollision method, thus, it is equal to "0 0", which is the center of the game world.

You could simply use a Global variable but this is bad practice in my opinion; you should instead use a dynamic variable in the PlayerShip namespace.

In TorqueScript, you can create dynamic variables for any object at any time. I suggest creating the dynamic variables when initially creating the object.

%spaceship = new Sprite(PlayerShip);
%spaceship.oldposition = "0 0";
OR
PlayerShip.oldposition = "0 0";

Now that the variable oldposition has been created, we need to access it when onCollision is called on our PlayerShip object.

function PlayerShip::onCollision(%this, %sceneobject, %collisiondetails)
{
%this.oldposition = %this.getPosition();
}

The variable %this refers to the instance of PlayerShip which has called onCollision.

Hope this makes things a bit clearer!
#2
03/29/2014 (12:06 am)
Ok, that does make sense. Sort of like defining a property for an object more than a variable.

Getting down to the practical use, then, you would use getPosition() and setPosition() to find and set location?

Also, I'm using set velocity to move, which appears to be physics based and might be a bit sloppy for my instance. Would it be better to use a similar method to move based on keystrokes? Ie, get the desired angle of motion, and then setposition() in that direction? I've never used a game engine with real physics before so I'm more used to writing them in and faking them. I really just need more practice but thank you for being willing to help me out!
#3
03/29/2014 (12:26 am)
Quote:you would use getPosition() and setPosition() to find and set location?

Exactly. GetPosition returns a value like "0 5"; setPosition takes the same kind of value if you want to manually pass it an argument.

If you want to see what the results are in the console, simply type in
echo(%MyObject.getPosition());

Never hesitate asking questions; the GG community is one of the most helpful around when it comes to helping newcomers.

Physics and movement


Torque2D is built on top of Box2D physics; the system is used for collisions, movement, picking, etc. If you were to use that system, base yourself on any of the many examples included in the Sandbox (the default project that ships with T2D) or the Getting Started guide.

You can also bypass the physics engine by using the following function on any object derived from SceneObject (Sprite, for example)

SceneObject.moveTo(x/y,speed, autostop, warptotarget);

For example =>
%MySceneObject.moveTo("50 15", 15.0, true, false);

This will smoothly move your object from its current position to the one passed as x/y in the function. Note that there is a similar "rotateTo" function.

You can find all script functions for an object in its corresponding _ScriptBinding.h. For SceneObjects, look in SceneObject_ScriptBinding.h.

Here's a link to the moveTo function in the source as an example.
#4
03/29/2014 (4:52 am)
Simon's responses are solid and correct. I do have one correction, though:

Quote:You can also bypass the physics engine by using the following function on any object derived from SceneObject (Sprite, for example)

moveTo doesn't bypass the physics engine. In the source code, the function uses box2d functions to apply force and move the object to the position specified via script.
#5
03/29/2014 (8:34 am)
Quote:base yourself on any of the many examples included in the Sandbox

Yeah I've been messing around with the trucktoy but the concept of joints also eludes me(though it seems incredibly powerful). I heard of someone making a platformer where the main character was essentially on an invisible unicycle for the physics to work correctly, which sounded like a good idea but I think I need more experience with the simple syntax first.
#6
03/29/2014 (10:08 am)
@Mich : You're right. Let's just say that with moveTo, you don't have to think about the physics engine. The correct force applied to the object is calculated automatically, ensuring that your object will reach its destination in the specified time, regardless of its physical properties of said object.

@Brandon : The wiki is (one of) your best friend(s)!

To familiarize yourself with the system, the TargetJoint might be your best bet.

%jointID = MyScene.createTargetJoint( %someObject, "0 0", 15.0 );

This example will anchor %someObject to world position "0 0", with a force of 15.0. The joint's ID will be stored in %jointID, which you can keep track of using a global variable or a dynamic variable in the namespace of the object.

Make sure that your object is not already at position "0 0" or you won't see the joint in action.

Also, if you want to visually follow what the joint is doing, set your scene's debug mode to "joints"

MyScene.setDebugOn("joints");

If you want to then change the anchor point of the joint...

MyScene.setTargetJointTarget( %jointID, "7 5" );

Just remember that all joints exist at the Scene level, not in the SceneObjects. %Myobject.setTargetJointTarget(...) won't work.
#7
04/01/2014 (9:02 pm)
Here's my update movement function. It's modified from the shooterControls from the tutorial. My character simply moves 1 unit in the direction selected and stays there. I think I need something that's WHILE the key is pressed this constantly updates?

function RPGControlsBehavior::updateMovement(%this)
{
	//Get the current position in case of collision
	%this.owner.oldPosX = %this.owner.getPositionX();
	%this.owner.oldPosY = %this.owner.getPositionY();
	
	//Move
    %this.owner.newPosX = %this.owner.oldPosX + (%this.right - %this.left);
    %this.owner.newPosY = %this.owner.oldPosY + (%this.up - %this.down);
    
    %newPos = %this.owner.newPosX SPC %this.owner.newPosY;
    %this.owner.setPosition(%newPos);
}