Game Development Community

Forces, T2DForceComponent and interfacing

by Kzoink · in Torque X 2D · 07/10/2007 (3:26 am) · 2 replies

What is the intended/best practice to set or animate the strength of a force on the T2DForceComponent? I assumed it would be through a ValueInterface as is stated in the documentation, "typical usage of a T2DProcessComponent.Processor is to convert game pad input into the rotation of a link point or the strength of a force," but after looking at code, API docs and forums, I still don't see a good way to do it. I could look up the ForceInstance in the ForceInstances property by finding its index (a real pain because there's no quick way to get the index) and then set the Strength property, but I would really like to be able to use the T2DProcessComponent or similarly designed custom components without creating a kludge. I am still digging but would appreciate any tips in this area.

I initially thought the Force or ForceInstance class would perhaps expose a ValueInterface for its current strength, but there was no such luck... The only interface related to forces is IT2DForceGenerator which is implemented by the T2DForceComponent. But that interface only has two functions: void PreUpdateForces(Move move, float dt) and void PostUpdateForces(Move move, float dt). Those don't look useful for setting the strength of my force. But the documentation implied there was a way to interface and to update the forces.

Just setting the strength is a really nasty process that has no easy shortcut as far as I can see. The easiest method I've seen so far is to remove the force, set the InitialStrength property, then re-add it... But that is not really a good way to animate or scale something because that is a lot of overhead.

My current idea has been to create a class called ForceInterface that keeps a reference to the T2DForceComponent and a force, and implements the ValueInterface interface in a couple properties that actually set the strength of the referenced force. Then, I override T2DForceComponent to make it create instances of my ForceInstance class and register their interfaces for each force.

This seems to work, as my ForceInterface class is getting accessed by the T2DProcessComponent every tick, but the value it is setting on my interface is 0.0f. I'll have to dig a little deeper.

#1
07/10/2007 (11:57 am)
T2DForceComponent isn't well documented yet, but it registers a float interface for each force which lets you control it. You can set the control parameter between 0.0f and 1.0f to control the intensity of the force.
// declare a float interface
ValueInterface<float> _controlMyForce;
// get interface that was registered by T2DForceComponent.
_controlMyForce = Owner.Components.GetInterface<ValueInterface<float>>("float", "TheNameOfMyForce");
// set force to 50%
_controlMyForce.Value = 0.5f;
#2
07/10/2007 (12:07 pm)
Ah-hah, so it does register forces as a float interface by default. I know I was not getting any errors when looking up an interface with the name of the force, but it didn't seem to do anything when I used it with a Processor and T2DProcessComponent. Maybe my problem is related to T2DProcessComponent then.

So I went ahead and created a custom solution only to find out it has already been done... That sucked... Hehe

Thanks Dan