Game Development Community

Adding gravity

by Devboy · in Torque X 2D · 01/19/2007 (12:34 am) · 6 replies

Hi,

Can anyone paste a snippet of how to add gravity to an object (assuming it has a Physics component)?

Thanks

#1
01/19/2007 (8:13 am)
Let me rephrase. What's wrong here?

TorqueObjectDatabase db = TorqueObjectDatabase.Instance;
TorqueBase o = db.FindObject("MyShip");
T2DStaticSprite s = o as T2DStaticSprite;
m_Ship = s.Clone() as T2DStaticSprite;
T2DForceComponent fc = new T2DForceComponent();
T2DForceComponent.Force f = new T2DForceComponent.Force();
f.InitialStrength = -10;
f.ConstantDirection = -1;
f.ConstantDirectionIsWorldSpace = true;
fc.AddForce(f);
m_Ship.Components.AddComponent(fc);
m_Ship.Components.AddComponent(new MyComponent()); 
TorqueObjectDatabase.Instance.Register(m_Ship);

I see the ship but it doesn't move!
#2
01/19/2007 (10:39 am)
OK got it,

I was missing

f.MinStrength = 1;
f.MaxStrength = 2;

It must have clamped the strength to the default (0), which effectively canceled it.

Also, direction is in degrees, not radians which is worth noting.
#3
01/24/2007 (10:29 am)
On a related note, I've attempted to create a custom 'GravityComponent' that would derive from T2DForceComponent and would be available in TGBX. The purpose of the 'GravityComponent' is to encapsulate the ability to add gravity to an object in TGBX at design time rather than at run time.

GravityComponent

public class GravityComponent: T2DForceComponent
{
        float m_fConstantDirection;
        bool m_bConstantDirectionIsWorldSpace;
        float m_fInitialStrength;
        float m_fMaxStrength;
        float m_fMinStrength;

        //Constructor
        public GravityComponent()
        {

        }

        protected override bool _InitComponent(TorqueObject owner)
        {
            if(!base._InitComponent(owner) || !(Owner is T2DSceneObject))
                return false;

            
            T2DForceComponent.Force force = new T2DForceComponent.Force();

            force.ConstantDirection = m_fConstantDirection;
            force.ConstantDirectionIsWorldSpace = m_bConstantDirectionIsWorldSpace;
            force.InitialStrength = m_fInitialStrength;
            force.MaxStrength = m_fMaxStrength;
            force.MinStrength = m_fMinStrength;

            AddForce(force);

            return true;
        }

        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);

            GravityComponent obj2 = (GravityComponent)obj;
            obj2.ConstantDirection = ConstantDirection;
            obj2.ConstantDirectionIsWorldSpace = ConstantDirectionIsWorldSpace;
            obj2.InitialStrength = InitialStrength;
            obj2.MaxStrength = MaxStrength;
            obj2.MinStrength = MinStrength;
        }

        public float ConstantDirection
        {
            get { return m_fConstantDirection; }
            set { m_fConstantDirection = value; }
        }

        public bool ConstantDirectionIsWorldSpace
        {
            get { return m_bConstantDirectionIsWorldSpace; }
            set { m_bConstantDirectionIsWorldSpace = value; }
        }

        public float InitialStrength
        {
            get { return m_fInitialStrength; }
            set { m_fInitialStrength = value; }
        }

        public float MaxStrength
        {
            get { return m_fMaxStrength; }
            set { m_fMaxStrength = value; }
        }

        public float MinStrength
        {
            get { return m_fMinStrength; }
            set { m_fMinStrength = value; }
        }
}

Registering the Custom Component

Paste this into your componentSpecs.ed.cs file to make the component available to TGBX. Make sure to change "StarterGame" found in the registerComponent(...) call to the namespace you're using for your game.

$GravComponent = TorqueX::registerComponent( "GravityComponent", "StarterGame" );
$GravComponent.addField("ConstantDirection", "Float", 0);
$GravComponent.addField("ConstantDirectionIsWorldSpace", "Bool", "true");
$GravComponent.addField("InitialStrength", "Float", 0);
$GravComponent.addField("MaxStrength", "Float", 0);
$GravComponent.addField("MinStrength", "Float", 0);

Note

As of TGBX v1.1.3 the value set for ConstantDirectionIsWorldSpace in TGBX does not appear to copy correctly back into the C# source file. I don't know if this is a bug with TGBX for Boolean values or something else. If I find the problem, and can correct it, I'll post it here. If anyone else can find the reason why the Boolean value isn't translating correctly I'd love to know why.

Hopefully this is of some use to someone. At the very least I hopes it gives people some ideas on how they can expose internal Torque Components to TGBX by wrapping them in a custom Component of their own.
#4
01/24/2007 (1:01 pm)
Good one.

Thanks!
#5
01/24/2007 (3:04 pm)
@BeerSkunk

Be careful with that AddForce(_f) call in _InitComponent. Cloning a T2DForceComponent will copy all the forces to the new component but your CopyTo method does not copy the force itself. So a new force is created in the cloned component and that new force is also added. Essentially, each time you clone and register a component that was a registered GravityComponent you will end up increasing the number of registered forces by one. This will probably still happen even if you account for copying the _f force because you still add it to the component every time. It is certainly possible and desirable to do what you're doing in concept. The execution needs tweaked a bit though.

#6
01/24/2007 (5:38 pm)
Good point.

I'm sure there's more pitfalls in the code I posted, and I appreciate the heads up.