Game Development Community

FxSceneObject.applyXXXForce

by Seth Willits · in Torque Game Builder · 03/06/2005 (5:03 pm) · 14 replies

One of the things I recognized was weird, is that there's no method to simply apply a force, you can only set the total force on an object. This doesn't seem right at all. If there's any point where you need have two constant forces at the same time, unless you do some sort of string manipulation using getConstantForce, the second constant force you set will obliterate the first. An applyConstantForce method where you can all it multiple times and it accumulates to create the current force after each call seems to be a better solution, if not necessary.

Consider that I have a rocket ship with boosters in the standard 8 directions and the keys 1, 3, 4, 6, 7, 8, 9 are used to turn off & on the booster in each of those directions. Were I to use setConstantForce to apply the booster forces, I'd need to have a single method that determines what constant force to apply. Something along the lines of:

function setBoosterForces() 
{
	$xDirection = 0;
	$yDirection = 0;
	
	
	if ($DLBooster) {
		$xDirection += 2.5;
		$yDirection += 2.5;
	}
	
	if ($DBooster) {
		$yDirection += 5.0;
	}
	
	if ($DRBooster) {
		$xDirection -= 2.5;
		$yDirection += 2.5;
	}
	
	if ($RBooster) {
		$xDirection -= 5.0;
	}
	
	if ($URBooster) {
		$xDirection -= 2.5;
		$yDirection -= 2.5;
	}
	
	if ($UBooster) {
		$yDirection -= 5.0;
	}
	
	if ($ULBooster) {
		$xDirection += 2.5;
		$yDirection -= 2.5;
	}
	
	if ($LBooster) {
		$xDirection += 5.0;
	}
	
	$ship.setConstantForce($xDirection SPC $yDirection);
}




function DLKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$DLBooster = %down;
	setBoosterForces();
}

function DKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$DBooster = %down;
	setBoosterForces();
}

function DRKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$DRBooster = %down;
	setBoosterForces();
}

function RKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$RBooster = %down;
	setBoosterForces();
}

function URKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$URBooster = %down;
	setBoosterForces();
}

function UKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$UBooster = %down;
	setBoosterForces();
}

function ULKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$ULBooster = %down;
	setBoosterForces();
}

function LKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
	} else {
		// stop sound
		// stop particle effect
	}
	$LBooster = %down;
	setBoosterForces();
}

#1
03/06/2005 (5:04 pm)
Where as if we had an applyConstantForce method, we'd simply need:

function DLKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("2.5 2.5");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("-2.5 -2.5");
	}
}

function DKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("0 5");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("0 -5");
	}
}

function DRKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("-2.5 2.5");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("2.5 -2.5");
	}
}

function RKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("-5 0");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("5 0");
	}
}

function URKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("-2.5 -2.5");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("2.5 2.5");
	}
}

function UKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("0 -5");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("0 5");
	}
}

function ULKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("2.5 -2.5");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("-2.5 2.5");
	}
}

function LKey(%down) {
	if (%down) {
		// play sound
		// start particle effect
		$ship.applyConstantForce("5 0");
	} else {
		// stop sound
		// stop particle effect
		$ship.applyConstantForce("-5 0");
	}
}

Am I wrong?
#2
03/06/2005 (5:05 pm)
After writing that I realize it was probably longer than it needed to be. :)
#3
03/06/2005 (9:00 pm)
Maybe I'm understanding incorrectly but couldn't you do something like:


%newX = getWord($ship.getConstantForce(), 0); // getWord(text, index) - returns a string
%newY = getWord($ship.getConstantForce(), 1) - 200; // going up

$ship.setConstantForce( %newX SPC %newY );


This worked when I tried it, though now that I look at it, it's not much different from what you're already doing... and you have to press the key again to add more boost, but that can probably be fixed using schedule or something.
#4
03/06/2005 (9:20 pm)
Well the string manipulation above basically is an "apply" type of method. I realized after I wrote the big long example code above, it wasn't all that good of an example, and just distracted from the point. Anyway, yes, you could do exactly that, but that should an additional built-in method. It's just.... logical. In the real world you don't set total forces, you apply them individually. Anyway so yeah, it's not as big of a deal as the length of the post sort of makes it out to be, but it doesn't seem right.
#5
03/06/2005 (9:36 pm)
What about using setImpulseForce and setConstantForce? They should do what need

Check this thread.

www.garagegames.com/mg/forums/result.thread.php?qt=26888

I hope I read you right
#6
03/06/2005 (9:40 pm)
You didn't. :^)

I'm talking about there should be an applyConstantForce in addition to setConstantForce.
#7
03/06/2005 (10:53 pm)
You know... you don't have to rely on things being in the engine. You can write them yourself in either C++ or even script. For example here is your applyConstantForce for fxStaticSprite2d:

function fxStaticSprite2D::applyConstantForce(%this,%x,%y)
{
	%origX = getWord(%this.getConstantForce(), 0);
	%origY = getWord(%this.getConstantForce(), 1);
	
	%this.setConstantForce(%origX + %x, %origY + %y);
}
#8
03/06/2005 (11:02 pm)
This is true.
#9
03/07/2005 (11:04 am)
The code above didn't work for me as posted (incorrect number of parameters for setConstantForce()). The function should read as follows:

function fxStaticSprite2D::applyConstantForce(%this, %x, %y) {

   %origX = getWord(%this.getConstantForce(), 0);
   %origY = getWord(%this.getConstantForce(), 1);
   
   %this.setConstantForce(%origX + %x @ " " @ %origY + %y);
}

Also, while it is easy to write this function, it's so useful that it would be nice to see it added to the engine.
#10
03/07/2005 (11:58 am)
@Chris: Yes, that was just a typo by LabRat.

Just to let you know, you can use the keywordSPC instead of @ " " @ to concatenate two strings with a space in-between. :)

- Melv.
#11
03/07/2005 (12:44 pm)
@Melv: I didn't know about the SPC keyword - very helpful :). Actually, I think I remember seeing it in the tutorial and not having a clue what it meant :P

I hope I didn't come across bad by posting the corrected function - being new to TorqueScript, the typo really threw me off, and I didn't want others with the same (limited) knowledge as me to have the same problems.

By the way, great job with T2D - I'm really enjoying it.
Chris
#12
03/07/2005 (12:47 pm)
@Chris: Not at all. I know that LabRat does that kind of thing just to keep people on their toes. He's evil that way. ;)

- Melv.
#13
03/07/2005 (2:12 pm)
Actually... it was a bit late and I never have quite gotten used to sending two seperate values as one string :)

That and it was never tested...

Oh and I like to present the "idea" sometimes more then just giving the solution... I'm a firm believer in the "Teach a man to fish" philosophy
#14
03/07/2005 (2:32 pm)
No, that doesn't sound quite right, I believe that you are simply evil. :)

- Melv.