Game Development Community

Rotating a sprite

by John Bura · in Torque X 2D · 06/17/2010 (11:09 am) · 13 replies

Hi I have checked the forums. I have trouble shot. But I cannot seem to get an object to just continually rotate.

Here is my code

T2DSceneObject circleRotate = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("CircleRotate");
            Vector2 angle = new Vector2(30, 30);
            circleRotate.Rotation = T2DVectorUtil.AngleFromVector(angle);

What am I doing wrong?

#1
06/17/2010 (11:53 am)
It's classified under the physics component.

T2DSceneObject x = new T2DSceneObject();
            x.Physics.AngularVelocity = 1;

Where 1 is the degrees per second you want it to turn.

The object must have a physics component for it to work I beleive.

#2
06/17/2010 (12:08 pm)
Awesome I just need to to reference an object I set up in TBX. Is this how I do it?

T2DSceneObject circleRotate = new T2DSceneObject();
            circleRotate = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("CircleRotate");
            circleRotate.Physics.AngularVelocity = 1;

#3
06/18/2010 (12:13 am)
If you don't need to change it on the fly you can actually just set it in TXB under the t2dphysicscomponent properties I believe.

If you do need to change it on the fly, then yes that should do the trick.

Just remember to make sure the object has a physics component attached.
#4
06/18/2010 (7:42 am)
What do you have to change in TBX to make it rotate. I just want to have an object rotate and stay rotating.

Also this code doesn't work.
T2DSceneObject circleRotate = circleRotate = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Rotate");

            if (circleRotate != null)
                circleRotate.Physics.AngularVelocity = 100;
What am I doing wrong?
#5
06/18/2010 (10:10 am)
Are you making sure the object has a T2DPhysicsComponent added to it? This can be done in TXB under the component tab. Once you add the physics component you will see the AngularVelocity property under the physicscomponent tab.

svn2.xp-dev.com/svn/Whee/rotation.png
#6
06/18/2010 (10:34 am)
T2DSceneObject circleRotate = circleRotate = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Rotate");  
  
            if (circleRotate != null)  
                circleRotate.Physics.AngularVelocity = 100;
Well for one, there is something erroneous about that code, you have = used twice, it should instead read this
T2DSceneObject circleRotate =  (T2DSceneObject)TorqueObjectDatabase.Instance.FindObject("Rotate");  
  
            if (circleRotate != null)  
                circleRotate.Physics.AngularVelocity = 100;

Make sure you also place a breakpoint to make sure your code is getting executed.
#7
06/18/2010 (10:43 am)
Hmmm

I have done this but the object just disappears when I Add anything to angular velocity.

Edit

Will I see that I used the equal sign twice. But The code still doesn't work.
#8
06/18/2010 (3:49 pm)
That's very strange, I just made an object in one of my levels with the angular velocity set in TXB and it spins at 20.

I also used that exact code to make it spin in the other direction with

T2DSceneObject x = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("bushy");
                  x.Physics.AngularVelocity = -20;

Is your object mounted?
#9
06/18/2010 (3:55 pm)
How do I mount an object?
#10
06/18/2010 (4:14 pm)
That will be a no then :P

Mounting is a whole other subject, lets get this rotation working first.

So, if you change the angular velocity the sprite dissapears?

Did you try setting the breakpoint to make sure that you get inside the if statement?

Where are you actually changing the angular velocity. Is it after a button press or just after the scene is loaded?
#11
06/18/2010 (4:48 pm)
Yeah if I use the Component in TBX the sprite just disappears.

I did try putting a break point. I Do not get inside the if.

I am changing the velocity when the level is loaded.
#12
06/18/2010 (5:54 pm)
My guess is that if your object is just "disapearing" its colliding with something. All objects collide by default with all other objects, so either remove the collision component or uncheck collisions enabled or something of that nature.
#13
06/19/2010 (5:27 am)
*SOLVED*

Thanks Will. It was colliding with something.