Game Development Community

Cloning Problem

by Viktor Rumanuk · in Torque X 2D · 02/11/2008 (4:01 pm) · 14 replies

I am adding shooting to the platformer framework, trying to use the mouse as the aimer instead of how its been done already. This is what I have so far:
public virtual void ProcessTick(Move move, float dt)
        {
                MouseState ms = Mouse.GetState();
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    Vector2 mousepos = new Vector2(ms.X, ms.Y);
                    T2DSceneObject bullet = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("Bullet");
                    bullet.Visible = true;
                    bullet.Position = _player.Position;
                    bullet.Layer = _player.Layer + 1;
                    bullet.Rotation = T2DVectorUtil.AngleFromTarget(bullet.Position, mousepos);
                    bullet.Physics.Velocity = T2DVectorUtil.VelocityFromTarget(bullet.Position, mousepos, 5.0f);
                } 
        }
The bullet does not appear at all. It doesn't seem like I'm missing anything looking at the tdn tutorial. Contents of if statement are being called.Can't get into tdn right now or I would double check.
Thanks for the help, Viktor

#1
02/12/2008 (6:18 am)
I know I had an issue with some things appearing because I did not set the size on my object. Just a thought.
#2
02/12/2008 (7:27 pm)
No luck with that, good piece of advice though. And on a totally unrelated note the official release date for Spore(finally) is September 7.
#3
02/24/2008 (8:56 am)
Still haven't figured this out. I know its a problem with cloning the object. I assume it should be a template that I clone from.
#4
02/24/2008 (9:00 am)
Totally a shot in the dark, but I always forget to set the scenegraph when I'm cloning in script.

Edit: hah - forgot that this was the TX forum...in TorqueX, I always forget to add clones to the database. :)
#5
02/24/2008 (9:40 am)
Hmm, didn't seem to help. Here is the code now:
public virtual void ProcessTick(Move move, float dt)
        {
            MouseState ms = Mouse.GetState();
            if (ms.LeftButton == ButtonState.Pressed)
            {
                Vector2 mousepos = new Vector2(ms.X, ms.Y);
                T2DSceneObject bullet = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("Bullet");
                bullet.Visible = true;
                bullet.Rotation = 0;
                bullet.Physics.Velocity = new Vector2(0, 0);
                bullet.Size = new Vector2(6, 6);
                bullet.Position = _player.Position;
                bullet.SceneGraph = _player.SceneGraph;
                bullet.Layer = _player.Layer + 1;
                bullet.Physics.Velocity = T2DVectorUtil.VelocityFromTarget(_player.Position, mousepos, 20.0f); 
            }
        }
#6
02/25/2008 (1:46 am)
Hmmm, this might be a dumb comment but ... I wonder if the bullet is colliding with the player since its position is the same?
#7
02/25/2008 (7:32 am)
Are you registering the object with the TorqueObjectDatabase?
#8
02/25/2008 (9:51 am)
That could be the problem Dan, I'll try it when I get home.
#9
02/25/2008 (3:42 pm)
That got them spawning, thanks Dan. I now notice that my rotation and velocity code don't seem to be working correctly.
public virtual void ProcessTick(Move move, float dt)
        {
            MouseState ms = Mouse.GetState();
                if (ms.LeftButton == ButtonState.Pressed && delayRemaining <= 0 || first == true && ms.LeftButton == ButtonState.Pressed)
                {
                    Vector2 mousepos = new Vector2(ms.X, ms.Y);
                    T2DSceneObject bullet = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("Bullet");
                    bullet.Visible = true;
                    bullet.Rotation = 0;
                    bullet.Physics.Velocity = _player.Physics.Velocity;
                    bullet.Position = _player.Position;
                    bullet.SceneGraph = _player.SceneGraph;
                    bullet.Layer = _player.Layer + 1;
                    bullet.Physics.Velocity = T2DVectorUtil.VelocityFromTarget(_player.Position, mousepos, 20.0f);
                    TorqueObjectDatabase.Instance.Register(bullet);
                    first = false;
                    delayRemaining = .5;
                }
                delayRemaining -= dt;
        }
#10
03/05/2008 (5:43 pm)
Wow, no-detail post, sorry. When the bullets spawn their rotation is always 0 and they always have the same velocity, which is south-eastish. I'm thinking its something to do with getting the mouse state, but thats just a guess.
Thanks, Viktor
#11
03/06/2008 (9:42 am)
@Viktor - The mouse position you're getting will be in window coordinates, not Torque world coordinates. You'll need to take into account the size of the GUISceneView (800 x 600, if you're using the defaults), the size of the camera (100.0 x 75.0 by default), and the position of the camera (by default, the camera is centered, but screen coordinates count from 0,0 at the upper left corner) in order to translate the window coordinates into torque world coordinates.
#12
03/13/2008 (2:12 pm)
Thanks. Now they are moving in the correct direction, rotation is still off though. This is what I'm using:
bullet.Rotation = T2DVectorUtil.AngleFromTarget(bullet.Position, MouseConvert(mousepos));
It doesn't seem like the rotation is just a set number of degrees off, looks more random to me.
#13
03/13/2008 (2:21 pm)
This may (or may not) be relevant:

/// <summary>
        /// Rotation of scene object.  Setting this property after the object has been registered
        /// will cause it to interpolate between current rotation and new rotation.  In order to
        /// make an object move to a new rotation without interpolation use WarpToPosition method.
        /// </summary>
        public virtual float Rotation
#14
03/13/2008 (2:36 pm)
Rotation is being set before registration, thanks though.