Moving A Sprite Around A Circle...
by David Taylor · in Torque Game Builder · 04/24/2006 (3:46 pm) · 10 replies
Hi there. I'm not entirely sure if this is the right forum to put this in, but here we go anyway. I have a sprite which I want to move around the screen in a circular fashion. I think the easiest way to envisage this is to say, imagine the circle as a railway track, and the sprite as a train... As I move the mouse, I want to be able to move that 'train' clockwise or anti-clockwise, but always have it facing the centre of the circle. I assume I'll need to use rotateTo() in this somewhere, but can anyone provide me the keys to make this work?
I'd also like to know if there's a way to work velocity by an angle, rather than setLinearVelocityX(), etc. I'd like to effectively make an object move forward in the direction it is facing, whatever angle that direction might be.
I'd also like to know if there's a way to work velocity by an angle, rather than setLinearVelocityX(), etc. I'd like to effectively make an object move forward in the direction it is facing, whatever angle that direction might be.
About the author
#2
Okay, so I've got setAutoRotation() working fine, and setLinearVelocityPolar() is such a relief to find, lol!
But I'm having trouble getting my sprite to move around in a circle. Could you show me that code?
04/24/2006 (6:42 pm)
Thanks, Jason. That was really helpful.Okay, so I've got setAutoRotation() working fine, and setLinearVelocityPolar() is such a relief to find, lol!
But I'm having trouble getting my sprite to move around in a circle. Could you show me that code?
#3
XPos = GraphicsWidth()/2 + Sin( Angle )*Radius
YPos = GraphicsHeight()/2 + Cos( Angle )*Radius
Also, to have your object face the middle point use the ATan2 function.
Easy as pie.
04/25/2006 (12:53 am)
This is a psuedo code of sorts.XPos = GraphicsWidth()/2 + Sin( Angle )*Radius
YPos = GraphicsHeight()/2 + Cos( Angle )*Radius
Also, to have your object face the middle point use the ATan2 function.
Easy as pie.
#4
Instead of having the object, ($hand), moving around in a circle, it is just rotating on the spot. It bobbles just a little, but that's all. And changing the radius pushes the sprite further away, to the bottom right.
Here's my code:
Those echo commands never output anything different - always the same position.
What did I do wrong?
04/25/2006 (6:34 pm)
Thanks for that, Daniel, but i've still somehow managed to get it wrong, lol.Instead of having the object, ($hand), moving around in a circle, it is just rotating on the spot. It bobbles just a little, but that's all. And changing the radius pushes the sprite further away, to the bottom right.
Here's my code:
function createImages()
{
// The code to create the hand sprite
$hand = new t2dStaticSprite() { scenegraph = t2dScene; };
$hand.setPosition("0 30");
$hand.setSize(10);
$hand.setImageMap(spriteHandImageMap);
}
function SceneWindow2D::onMouseMove(%this, %modifier, %worldPos, %mouseClicks)
{
%oldMousePositionX = $mouseObj.getPositionX();
// set the position of the object that is designated to follow the mouse
$mouseObj.setPosition(%worldPos);
// Get the hand to move around the edge of the circle
$hand.setRotation($hand.getRotation() + 1);
$hand.setLinearVelocityPolar($hand.getRotation(), 10);
$hand.setPositionX(10 / 2 + mAsin($hand.getRotation) * 10);
$hand.setPositionY(10 / 2 + mAcos($hand.getRotation) * 10);
echo($hand.getPositionX());
echo($hand.getPositionY());
}Those echo commands never output anything different - always the same position.
What did I do wrong?
#5
04/25/2006 (7:36 pm)
Oops! Apologies for the double post from my trigger happy finger! :P
#6
04/27/2006 (1:37 pm)
A much simpler, but not intuitive solution: Put an invisible sprite "guide" on your scene. Set the guide sprite rotating. Mount() the hand sprite to the guide sprite. Et voila the hand will go in a circle :-)
#7
04/27/2006 (1:38 pm)
Of course the mount point on the guide sprite will need to be offset from the center of the sprite.
#8
...except for one tiny thing, lol...
...I've set the "guide" to invisible with setVisible(false) - that has, however, also made the hand invisble. Is there a way to script the "guide" to be invisible, but the hand to be visible? ($hand.setVisible(true) had no effect.)
I no that using a transparent sprite as the "guide" would work, too, but I'm just wondering if there's a scripting way. :)
04/27/2006 (7:44 pm)
Thanks, Alex! That is where I've made the most progress so far. It turns in the circle just like I want! :D...except for one tiny thing, lol...
...I've set the "guide" to invisible with setVisible(false) - that has, however, also made the hand invisble. Is there a way to script the "guide" to be invisible, but the hand to be visible? ($hand.setVisible(true) had no effect.)
I no that using a transparent sprite as the "guide" would work, too, but I'm just wondering if there's a scripting way. :)
#9
04/27/2006 (8:42 pm)
David, if I recall correctly, 1 parameter of the mount() it to inherit properties from the parent. So if you turn off inheritance , then you should be good to go!
#10
04/27/2006 (11:42 pm)
Ah yes, there it is. I probably should have read the description of it a bit more closely. ;)
Torque Owner Jason Cahill
Default Studio Name
To update the rotation of the sprite use .autoRotation(degreesPerSecond);
For my game, which is doing something similar to what you are suggesting, I set up a 20 fps schedule callback and in each pass through, I set the new rotation (which is some fixed rotation rate either clockwise or counter-clockwise) and then I use %sprite.setLinearVelocityPolar(%sprite.getRotation(), %speed);
I hope that helps. If not, let me know and I can post some actual working code.