Smoothly move a shape using script?
by Yannick Lahay · in Torque Game Engine · 05/01/2007 (2:50 pm) · 29 replies
After searching and testing, I haven't found any way to move a shape smoothly.
Here's an example of a scripted animation I'd like to code:
(my shape is a rock)
- it moves left
- pause 1sec
- it moves right
- pause 1sec
and so on.
I also need a variable to change the speed of the move, because maybe the speed will change depending of the level being played.
It seems to be easy to code, but since I'm a beginner... actually it's not easy for me lol
Thank you for any help.
Here's an example of a scripted animation I'd like to code:
(my shape is a rock)
- it moves left
- pause 1sec
- it moves right
- pause 1sec
and so on.
I also need a variable to change the speed of the move, because maybe the speed will change depending of the level being played.
It seems to be easy to code, but since I'm a beginner... actually it's not easy for me lol
Thank you for any help.
#2
05/02/2007 (5:18 am)
There is a resource on rigid shapes that allow you to push stuff, but I don't know how you would move an object in script.
#3
Here's a couple of resources that might give you some idea of how to programmatically move an object:
Script-based object rotation
PathShape
Another option is to move the object via an animation. I could show you a cheap scripted hack on how to move an object via script but I doubt it would be very smooth or reliable.
05/02/2007 (5:55 am)
The rigidShape class has made its way into the stock build of Torque (it's just a trimmed down hoverVehicle). This rigidShape class uses physics/impulses to move shapes around, probably not what you're looking for. Here's a couple of resources that might give you some idea of how to programmatically move an object:
Script-based object rotation
PathShape
Another option is to move the object via an animation. I could show you a cheap scripted hack on how to move an object via script but I doubt it would be very smooth or reliable.
#4
05/02/2007 (2:53 pm)
Ok, thank you very much Caleb and Tim! :D
#5
05/02/2007 (3:06 pm)
I think I'll expand my resource. It would not be hard to add a trapezoidal acceleration profile control, s=1/2at^2, to the rotation, so that the object started rotating slowly, sped up (accelerated) and then decelerated to the final angle. And why not apply that to the other x,y,z axis as well so that rigid objects can be magically transported about with increasing speed and then slowed to settle at a final resting spot. I think it could be useful for certain effects with objects that do not have any other physics to control their movement.
#6
I think that moving an object programmatically with acceleration and deceleration is very useful for any platform game, or adventure game.
I'm very happy you help me! :)
I'm not at home atm, so I'll make the tests tomorrow.
I hope I'll manage to make it move :D
05/02/2007 (3:28 pm)
It's a very good idea!!I think that moving an object programmatically with acceleration and deceleration is very useful for any platform game, or adventure game.
I'm very happy you help me! :)
I'm not at home atm, so I'll make the tests tomorrow.
I hope I'll manage to make it move :D
#7
function MoveShape(%obj, %steps)
{
%obj.stepsToDo = %steps;
if(%obj.stepsToDo == 0)
return;
%obj.setTransform(vectoradd(%obj.getposition(), "0.3 0 0");
schedule(300, 0, "moveShape", %obj, %steps--);
}
05/02/2007 (4:02 pm)
Could't it be done like sofunction MoveShape(%obj, %steps)
{
%obj.stepsToDo = %steps;
if(%obj.stepsToDo == 0)
return;
%obj.setTransform(vectoradd(%obj.getposition(), "0.3 0 0");
schedule(300, 0, "moveShape", %obj, %steps--);
}
#8
05/02/2007 (4:07 pm)
Don't even bother with this using Torque physics. You need ODEScript or something like that, period.
#9
05/02/2007 (4:17 pm)
Anton, I don't think so. I would just move the shape according to simple motion laws relating acceleration, velocity and position so that it looks realistic and unsurprising and doesn't take forever to move and doesn't move abruptly. There is no mass involved, so no need for physics.
#10
Then, I'll try to implement an acceleration and a deceleration. It can be a good resource I think ^^
I don't think we need ODEScript, this is just a question of vectors, nothing to do with physics.
05/03/2007 (5:39 am)
@Vincent: I'll try your code, I think it might work. Thank you very much :)Then, I'll try to implement an acceleration and a deceleration. It can be a good resource I think ^^
I don't think we need ODEScript, this is just a question of vectors, nothing to do with physics.
#11
I really want the object to slide on the ground :)
I'll try to do it with an animation sequence, if it's too difficult to script. But I'm very interested by how to script animations, for some items or some things in the environment, so I have a better control of the interactivity and I can change the animation in real time by changing variables.
05/04/2007 (2:24 pm)
@Vincent: I've just tried your code, and the movement it's not smooth.I really want the object to slide on the ground :)
I'll try to do it with an animation sequence, if it's too difficult to script. But I'm very interested by how to script animations, for some items or some things in the environment, so I have a better control of the interactivity and I can change the animation in real time by changing variables.
#12
Now I need to define a datablock (class) to hold the working variables and serve up the methods. One way to use it will be to create one of these and use its methods to move some existing shape around. Another way to use it is to derive something you want to be movable from this type and use the methods to move it around.
I intend to finish it this weekend.
Stay tuned, it might work.
05/04/2007 (2:45 pm)
I wrote the majority of the methods needed last night. I created a script based system of moving any object for which you have an id or name. It allows you to accelerate objects smoothly and bring them to rest at a new location, in 3 dimensions. Of course you'll be able to run things through the ground or leave them in the air. But hey that's part of the fun. Objects with real physical properties, like mass, would then fall to the ground I suppose.Now I need to define a datablock (class) to hold the working variables and serve up the methods. One way to use it will be to create one of these and use its methods to move some existing shape around. Another way to use it is to derive something you want to be movable from this type and use the methods to move it around.
I intend to finish it this weekend.
Stay tuned, it might work.
#13
05/04/2007 (3:14 pm)
Ok I stay tuned! Thanks a lot Tim! It's gonna be a nice resource :)
#14
05/05/2007 (12:33 am)
Have you tried finetuning my code? maybe a shorter pause in the schedule or move the object some more per step?
#15
05/05/2007 (12:39 am)
The setTransform method only allows you to move objects in increments of 0.1. You cannot go any lower which is why it is very difficult to obtain a smooth movement through script alone.
#16
05/05/2007 (12:40 am)
Ok thanks for the info
#17
05/05/2007 (4:50 am)
Tim, I never knew that setTransform had a resolution of only .1, is that right? If so, it sounds very odd. And implies you can only place objects in the world to within .1 units of intended position.
#18
The world editor must use a different method to position objects, maybe that can be tapped into for moving an object via script?
05/05/2007 (5:55 am)
Well I wrote a little scripted method to move an object via script using the setTransform function and the smallest increment I could use was 0.1.The world editor must use a different method to position objects, maybe that can be tapped into for moving an object via script?
#19
05/05/2007 (5:57 am)
P.S. if you don't believe me try it. Open a world, raise the editor, pick an object and take note of its ID. Use the console to move it via "setTransform" and you will see that increments below 0.1 are ignored and replaced with 0.1.
#20
05/05/2007 (6:02 am)
Ok, thanks, I'll have to look into that right away today as I have a bunch of code that I haven't tested that's going to be worthless at that resolution.
Torque Owner Yannick Lahay
It would be sufficient for me :)
Thanks !