using ParticlePlayer
by John Wilson · in Torque 2D Beginner · 10/11/2013 (10:32 am) · 11 replies
Hi folks, first time using T2D here. I did the Getting Started guide and finished the little asteroids game so I decided to add to it. I'm trying to attach a thrust particle whenever the ship is accelerating. I looked at the TruckToy at the exhaust pipe for this.
I got the particle attached and I can have it playing from spawn continuously but I can't get it to only emit while accelerating. At the end of my createSpaceShip() function I have this:
I haven't added code to stop playing when W is released yet. I can post the full code if needed, but this just builds on the tutorial code.
Any help?
I got the particle attached and I can have it playing from spawn continuously but I can't get it to only emit while accelerating. At the end of my createSpaceShip() function I have this:
// create the thrust particles %thrustParticles = new ParticlePlayer( ShipThrust ); %thrustParticles.setPosition( %posX-3, %posY ); %thrustParticles.setSceneLayer( 1 ); %thrustParticles.Particle = "TestModule:Exhaust"; %thrustParticles.SizeScale = 2; %thrustParticles.ForceScale = 0.4; %thrustParticles.EmissionRateScale = 8; myScene.add( %thrustParticles ); // this starts it playing %thrustParticles.stop(); // so let's switch it off // attach the thrust %joint = myScene.createRevoluteJoint( PlayerShip, %thrustParticles, "0.0 -2.0", "0 0" ); myScene.setRevoluteJointLimit( %joint, 0, 0 );And at the top of function PlayerShip::accelerate( %this ):
ShipThrust.play();This doesn't play at all. I did try it with all the particle code in the accelerate function but as that function is constantly called it just created tons of particles over and over. I've read the ParticlePlayer Guide page and can't seem to figure this out.
I haven't added code to stop playing when W is released yet. I can post the full code if needed, but this just builds on the tutorial code.
Any help?
#2
I'm using 2.0 which is listed as the master. If that is the problem then I'm super relieved for now, I thought I was just that bad. I'm compiling the engine myself, is their specific source files available for new fixes not officially released?
10/11/2013 (12:08 pm)
Thanks, I'm sure I'll enjoy the company :-) I'm using 2.0 which is listed as the master. If that is the problem then I'm super relieved for now, I thought I was just that bad. I'm compiling the engine myself, is their specific source files available for new fixes not officially released?
#3
10/11/2013 (2:28 pm)
If you want to keep up with the latest bug fixes and new features, switch the branch on github from master to development, download the zip or clone the branch, and compile. That's the easiest way to stay up to date, plus having more people use the development branch helps us test and push official releases faster.
#4
Only a new problem has occurred; when I hit W to start thrusting, the ship goes rightwards instead. If I rotate and thrust in different directions it behaves similarly, going in opposite directions. I tested this on the tutorial code the author provided too for the asteroid game and it's the same. It must be something in the engine?
If it helps, I compiled with the VS2012 solution provided and tested both Release and Debug versions. It was a clean build except for 2 warnings, I closed the log now but if I recall it was a warning of conversion from int to F32.
10/12/2013 (4:11 am)
Thanks, I just downloaded the dev. branch and got the thrust working :)Only a new problem has occurred; when I hit W to start thrusting, the ship goes rightwards instead. If I rotate and thrust in different directions it behaves similarly, going in opposite directions. I tested this on the tutorial code the author provided too for the asteroid game and it's the same. It must be something in the engine?
If it helps, I compiled with the VS2012 solution provided and tested both Release and Debug versions. It was a clean build except for 2 warnings, I closed the log now but if I recall it was a warning of conversion from int to F32.
#5
en.wikipedia.org/wiki/Polar_coordinates#Conventions
10/12/2013 (5:45 am)
That's not your fault either! Sorry, the tutorial needs to be updated. Changes were made to the polar coordinate system as there were a lot of inconsistencies. Before 0 degrees was along the +Y axis for some objects, along the +X for particles, and along the -Y axis for other objects. Now everything uses the standard convention where 0 degrees is along +X axis.en.wikipedia.org/wiki/Polar_coordinates#Conventions
#6
My knowledge of coordinates goes as far as everything works in 360 degrees and x is horizontal and y vertical. I'm not sure what along the +y or -x means.
:/
10/12/2013 (7:21 am)
I can't seem to figure out how to fix it. I tried tweaking with it but it doesn't help. Is there an up-to-date guide to help me understand how these coordinates work? I'm not really knowledged in maths notation.My knowledge of coordinates goes as far as everything works in 360 degrees and x is horizontal and y vertical. I'm not sure what along the +y or -x means.
:/
#7
to
As an aside, the acceleration code is much more complicated than it needs to be in my opinion. If I get a chance to do an improvement pass on this guide, I'll simplify it.
10/12/2013 (2:14 pm)
Well, if your object or ship is at the center of the screen, +x is screen right and +y is screen up. To be honest, I have never done the getting started tutorial, but looking at the script for accelerating the ship, I would try changing this line://Get the angle of our spaceship. When the ship is pointing upwards, its Angle is 0. %adjustedAngle = %this.Angle;
to
//Get the angle of our spaceship. When the ship is pointing upwards, its Angle is 90. %adjustedAngle = %this.Angle + 90;
As an aside, the acceleration code is much more complicated than it needs to be in my opinion. If I get a chance to do an improvement pass on this guide, I'll simplify it.
#8
I'm worried I will never get anywhere with this engine. I have no excuse to complain because I love what you guys have given us, for free too, but the tutorials are the starting point for new people like myself coming to T2D - the first thing we will do and getting stuck on it and having outdated documents is very off putting and makes it very awkward for us to say hello to T2D. :(
I've added a echo prints for the code to show whats going on too, still confusing.
I've read through line by line and understand what is being done now, but not why. Under the Global Namespace Ref. for Vector2Direction it lists no return value but we're assigning the result of it??
Is there any documents or examples on all those Vector2______ functions? When I see vector I think of C++ std::vector.
Sorry if I'm getting on your nerves or nagging it just seems like the logic behind this will be widely used for player controlled games so I really want to understand this as there are no toys demonstrating this and now the coordinates are changed all old code would be broke (?).
10/15/2013 (9:08 am)
Hello again, I've been trying to figure this out on and off for the past few days and it has just been stressing me out. I looked up on polar and I understand that now. I changed that line you said but the ship drives backwards. Naturally, I tried + 270 instead which does make it go forwards, and if I face downwards, W will push me in that direction too as expected... but the sides are inverted: facing right and pushing W will make me go left, and the reverse same for facing left. Also note you said when I face up (as when spawning) the angle will be 90, but using "echo( PlayerShip.Angle )" surprisingly it is still 0.I'm worried I will never get anywhere with this engine. I have no excuse to complain because I love what you guys have given us, for free too, but the tutorials are the starting point for new people like myself coming to T2D - the first thing we will do and getting stuck on it and having outdated documents is very off putting and makes it very awkward for us to say hello to T2D. :(
I've added a echo prints for the code to show whats going on too, still confusing.
function PlayerShip::accelerate( %this )
{
echo( " " NL " " NL " " );
ShipThrustParticles.play();
// Get the angle of our spaceship. When the ship is pointing upwards, its Angle is 0.
%adjustedAngle = %this.Angle + 270;
echo( %adjustedAngle SPC ": %adjustedAngle after : %adjustedAngle = %this.Angle;");
// Make sure that the angle is always between 0 and 360 degrees
if ( %adjustedAngle < 0 )
{
%adjustedAngle *= -1; // convert negative to positive
echo( %adjustedAngle SPC ": %adjustedAngle after : %adjustedAngle *= -1;");
}
else if ( %adjustedAngle > 0 )
{
%adjustedAngle = 360 - %adjustedAngle;
echo( %adjustedAngle SPC ": %adjustedAngle after : %adjustedAngle = 360 - %adjustedAngle;");
}
%adjustedAngle %= 360; // reason (just to remove decimal?) ???
echo( %adjustedAngle SPC ": %adjustedAngle after : %adjustedAngle %= 360;");
// If we are thrusting, shorten our vector
if ( %this.isThrusting )
{
// Calculate a direction from an Angle and Magnitude
%ThrustVector = Vector2Direction( %adjustedAngle, 35 );
echo( %ThrustVector SPC ": %ThrustVector after : %ThrustVector = Vector2Direction( %adjustedAngle, 35 );");
echo( %adjustedAngle SPC ": %adjustedAngle after : %ThrustVector = Vector2Direction( %adjustedAngle, 35 );");
}
else
{
%ThrustVector = Vector2Direction( %adjustedAngle, 95 );
echo( %ThrustVector SPC ": %ThrustVector after : %ThrustVector = Vector2Direction( %adjustedAngle, 95 );");
echo( %adjustedAngle SPC ": %adjustedAngle after : %ThrustVector = Vector2Direction( %adjustedAngle, 95 );");
// We temporarily remove the Damping of Linear Velocity to allow full power!
%this.setLinearDamping( 0.0 );
// echo( %this.getLinearDamping() SPC ": LinearDamping after : %this.setLinearDamping( 0.0 );");
// We temporarily increase the Damping of Angular velocity so that the ship turns slower when at full thrust
%this.setAngularDamping( 2.0 );
// echo( %this.getAngularDamping() SPC ": AngularDamping after : %this.setAngularDamping( 2.0 );");
}
// Adding our position to the ThrustVector determines the strength of our thrust
%MywordX = %this.Position.x + %ThrustVector.x;
%MywordY = %this.Position.y + %ThrustVector.y;
// applyLinearImpulse pushes on our spaceship, using %ThrustVector as the impulse vector.
// The second parameter is the point in the ship's collision shape used to apply the thrust
%this.applyLinearImpulse( %ThrustVector, "0 0" );
// We are now thrusting, we will set this to false when we release the 'w' key
%this.isThrusting = true;
// We create a schedule to repeat this thrust every 100 milliseconds
// this will call PlayerShip::accelerate continously, until we release the 'w' key
%this.thrustschedule = %this.schedule( 100, accelerate );
}I've read through line by line and understand what is being done now, but not why. Under the Global Namespace Ref. for Vector2Direction it lists no return value but we're assigning the result of it??
Is there any documents or examples on all those Vector2______ functions? When I see vector I think of C++ std::vector.
Sorry if I'm getting on your nerves or nagging it just seems like the logic behind this will be widely used for player controlled games so I really want to understand this as there are no toys demonstrating this and now the coordinates are changed all old code would be broke (?).
#9
Sorry to hear that you are struggling. I totally understand your viewpoint and definitely agree the tutorials should work and be easy to understand. The only minor thing I would add to T2D's defence is that the tutorial is written for the master branch version 2.0. By using the development branch, as I pointed you to, you get the latest bug fixes, but also major changes that are not reflected yet in the documentation. That is the price to pay, so to speak, for working on the bleeding edge. So I apologize for adding to your confusion.
I don't know how familiar you are with behaviors yet, so I might only make things even worse, but here is a behavior that I made for a similar type of movement. You won't be able to just swap code, but I wanted to show that by using setLinearVelocityPolar(), it saves you from a lot of the vector math. I should also point out that holding a key down does not give you constant velocity. In the meantime, I will try to write a quick accelerate function that directly applies to the getting started tutorial.
10/15/2013 (11:45 am)
Hi John,Sorry to hear that you are struggling. I totally understand your viewpoint and definitely agree the tutorials should work and be easy to understand. The only minor thing I would add to T2D's defence is that the tutorial is written for the master branch version 2.0. By using the development branch, as I pointed you to, you get the latest bug fixes, but also major changes that are not reflected yet in the documentation. That is the price to pay, so to speak, for working on the bleeding edge. So I apologize for adding to your confusion.
I don't know how familiar you are with behaviors yet, so I might only make things even worse, but here is a behavior that I made for a similar type of movement. You won't be able to just swap code, but I wanted to show that by using setLinearVelocityPolar(), it saves you from a lot of the vector math. I should also point out that holding a key down does not give you constant velocity. In the meantime, I will try to write a quick accelerate function that directly applies to the getting started tutorial.
if (!isObject(MoveBehavior))
{
%template = new BehaviorTemplate(MoveBehavior);
%template.addBehaviorField(upKey, "Key to bind to acceleration", keybind, "keyboard up");
%template.addBehaviorField(downKey, "Key to bind to deceleration", keybind, "keyboard down");
%template.addBehaviorField(leftKey, "Key to bind to rotate left", keybind, "keyboard left");
%template.addBehaviorField(rightKey, "Key to bind to rotate right", keybind, "keyboard right");
%template.addBehaviorField(acceleration, "Forward acceleration", float, 30.0);
%template.addBehaviorField(turnSpeed, "Velocity of turning", float, 150.0);
%template.addBehaviorField(damping, "Amount to damp movement", float, 1.0);
}
//-----------------------------------------------------------------------------
function MoveBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
%this.owner.setLinearDamping(%this.damping);
moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
//-----------------------------------------------------------------------------
function MoveBehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;
moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), %this);
moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
//-----------------------------------------------------------------------------
function MoveBehavior::onMoveUpDown(%this)
{
%this.owner.setLinearVelocityPolar(%this.owner.getAngle() + 90, (%this.up - %this.down) * %this.acceleration);
}
//-----------------------------------------------------------------------------
function MoveBehavior::onMoveLeftRight(%this)
{
%this.owner.setAngularVelocity((%this.left - %this.right) * %this.turnSpeed);
}
//-----------------------------------------------------------------------------
function MoveBehavior::moveUp(%this, %val)
{
%this.up = %val;
if (%val == 0)
return;
%this.onMoveUpDown();
}
//-----------------------------------------------------------------------------
function MoveBehavior::moveDown(%this, %val)
{
%this.down = %val;
if (%val == 0)
return;
%this.onMoveUpDown();
}
//-----------------------------------------------------------------------------
function MoveBehavior::moveLeft(%this, %val)
{
%this.left = %val;
%this.onMoveLeftRight();
}
//-----------------------------------------------------------------------------
function MoveBehavior::moveRight(%this, %val)
{
%this.right = %val;
%this.onMoveLeftRight();
}
#10
Basically, I added the 90 degrees since the spaceship image points up by default and not to the right. Then I got rid of the code that kept the angle between 0 and 360 degrees since the modulo operator does that anyway. Now it always works.
10/15/2013 (12:52 pm)
To fix the acceleration function to work with the development branch, I changed it to the following:function PlayerShip::accelerate(%this)
{
//Get the angle of our spaceship. When the ship is pointing upwards, its Angle is 90.
%adjustedAngle = %this.Angle + 90;
//When used as a math operand, % refers to modulo (or modulus) operator
//This function can be read as %adjusted angle = %adjustedAngle % 360;
%adjustedAngle %= 360;
//If we are thrusting, shorten our vector
if(%this.isThrusting)
{
//Calculate a direction from an Angle and Magnitude
%ThrustVector= Vector2Direction(%adjustedAngle,35);
}
else
{
%ThrustVector = Vector2Direction(%adjustedAngle,95);
//We temporarily remove the Damping of Linear Velocity to allow full power!
%this.setLinearDamping(0.0);
//We temporarily increase the Damping of Angular velocity so that the ship turns slower when at full thrust
%this.setAngularDamping(2.0);
}
//Adding our position to the ThrustVector determines the strength of our thrust
%MywordX = %this.Position.x + %ThrustVector.x;
%MywordY = %this.Position.y + %ThrustVector.y;
//applyLinearImpulse pushes on our spaceship, using %ThrustVector as the impulse vector.
//The second parameter is the point in the ship's collision shape used to apply the thrust
%this.applyLinearImpulse(%ThrustVector, "0 0");
//We are now thrusting, we will set this to false when we release the 'w' key
%this.isThrusting = true;
//We create a schedule to repeat this thrust every 100 milliseconds
%this.thrustschedule = %this.schedule(100,accelerate);
}Basically, I added the 90 degrees since the spaceship image points up by default and not to the right. Then I got rid of the code that kept the angle between 0 and 360 degrees since the modulo operator does that anyway. Now it always works.
#11
I'm in the middle of learning behaviors now so that extra code you posted is also much appreciated!
Your right about T2D's defense. I'd rather stay up-to-date though so there are no surprises later. On that note, is there any other major changes I need to be aware of? Just so I don't get a headache working with 2.0 tutorials thinking I suck! :)
10/18/2013 (4:47 am)
Hi Mike, much thank-yous! I cannot believe it was that simple. I thought the modulo there was simply removing the fractional part (as echoing the values suggested so) rather then repeating the previous logic. I'm in the middle of learning behaviors now so that extra code you posted is also much appreciated!
Your right about T2D's defense. I'd rather stay up-to-date though so there are no surprises later. On that note, is there any other major changes I need to be aware of? Just so I don't get a headache working with 2.0 tutorials thinking I suck! :)
Associate Mike Lilligreen
Retired T2Der
Are you using the master branch? There is a bug that prevents stopped particle players from playing again. This is fixed now in the development branch and will be included in the 3.0 release.
github.com/GarageGames/Torque2D/issues/64