Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Turning around a single moving point.
- TranquilityEden
-
7 posts
Turning around a single moving point.
Hello all,
I am trying to create a test rig for a lego robotics project (using brick pi). The robot has tank tracks. I would like to be able to program a sprite for each tank track that means that if one runs forward and one runs backwards the sprites will revolve around a centre point (robot body).

I am sorry if I haven't explained the problem very well!
Any help greatly appreciated!
Tranq
I am trying to create a test rig for a lego robotics project (using brick pi). The robot has tank tracks. I would like to be able to program a sprite for each tank track that means that if one runs forward and one runs backwards the sprites will revolve around a centre point (robot body).

I am sorry if I haven't explained the problem very well!
Any help greatly appreciated!
Tranq
Last edited by TranquilityEden (March 3, 2015 14:45:08)
- deck26
-
1000+ posts
Turning around a single moving point.
Does turning always mean one track going forwards and the other backwards? If so you just control the body and make the tracks follow it. Each track is in a fixed position relative to the body.
For example if the body is pointing right (90 degrees) the centre of the left track is, say T pixels away from the centre of the body in direction 0 degrees with the right track the same distance in direction 180 degrees. Both tracks point in the same direction as the body.
If the body is pointed at 45 degrees the tracks are still the same distance and still point in the same direction as the body but the positions are now T pixels in directions -45 degrees and 135 degrees.
So you can use trig to work out the track positions. If you want the tracks to actually move (eg by costume changing) you just need to know if the body is rotating clockwise or anti-clockwise.
For example if the body is pointing right (90 degrees) the centre of the left track is, say T pixels away from the centre of the body in direction 0 degrees with the right track the same distance in direction 180 degrees. Both tracks point in the same direction as the body.
If the body is pointed at 45 degrees the tracks are still the same distance and still point in the same direction as the body but the positions are now T pixels in directions -45 degrees and 135 degrees.
So you can use trig to work out the track positions. If you want the tracks to actually move (eg by costume changing) you just need to know if the body is rotating clockwise or anti-clockwise.
Last edited by deck26 (March 3, 2015 15:04:12)
- TranquilityEden
-
7 posts
Turning around a single moving point.
Does turning always mean one track going forwards and the other backwards? If so you just control the body and make the tracks follow it. Each track is in a fixed position relative to the body.
For example if the body is pointing right (90 degrees) the centre of the left track is, say T pixels away from the centre of the body in direction 0 degrees with the right track the same distance in direction 180 degrees. Both tracks point in the same direction as the body.
If the body is pointed at 45 degrees the tracks are still the same distance and still point in the same direction as the body but the positions are now T pixels in directions -45 degrees and 135 degrees.
So you can use trig to work out the track positions. If you want the tracks to actually move (eg by costume changing) you just need to know if the body is rotating clockwise or anti-clockwise.
Hello and thanks for your reply!
I was hoping to be able to change the speed of the tracks so that you could control how tight a curve the tank takes (much as you would with a real tank). I will investigate your ideas further today if I get a chance.
Thanks again.
Tranq
- deck26
-
1000+ posts
Turning around a single moving point.
Thinking about it further perhaps you can get somewhere by looking at the relative speeds of the tracks.
If both are at the same speed with one in the reverse direction (count that as negative) then the combined forward speed is 0 but both those speeds imply turning in the same direction.
If both at forwards at the same speed the forward speed is that speed. That may suggest that the forward speed is (track1 + track2) / 2
I wonder if the turning amount is something like track1 - track2. So if track1 alone implies turning clockwise a minus value for track2 increases the turning but a positive value decreses it.
I'll leave you to investigate further but something along those lines looks hopeful.
If both are at the same speed with one in the reverse direction (count that as negative) then the combined forward speed is 0 but both those speeds imply turning in the same direction.
If both at forwards at the same speed the forward speed is that speed. That may suggest that the forward speed is (track1 + track2) / 2
I wonder if the turning amount is something like track1 - track2. So if track1 alone implies turning clockwise a minus value for track2 increases the turning but a positive value decreses it.
I'll leave you to investigate further but something along those lines looks hopeful.
- TranquilityEden
-
7 posts
Turning around a single moving point.
Thinking about it further perhaps you can get somewhere by looking at the relative speeds of the tracks.
If both are at the same speed with one in the reverse direction (count that as negative) then the combined forward speed is 0 but both those speeds imply turning in the same direction.
If both at forwards at the same speed the forward speed is that speed. That may suggest that the forward speed is (track1 + track2) / 2
I wonder if the turning amount is something like track1 - track2. So if track1 alone implies turning clockwise a minus value for track2 increases the turning but a positive value decreses it.
I'll leave you to investigate further but something along those lines looks hopeful.
Thank you for your help deck26!
I will investigate what you have suggested. As I currently see this being applied the difference between the track speeds would provide the severity of the turn. Therefore, Track A runnign at speed = 5 (forwards) and Track B running at speed = -5 (backwards) would provide a difference of 10 (a tight turn), where as Track A running at speed = 5 (forwards) and Track B running at speed = 4 (forwards) would still provide a turn with a difference of 1 (a weaker turn). I will need to look at how to work out which direction the turn is in but that should be easy enough.
Could you possibly explain how to “tie” the track sprites to the body sprite in a bit more detail as I struggled to understand what you have written above.
Thanks again!
Tranq
- deck26
-
1000+ posts
Turning around a single moving point.
Direction of turn should be obvious from the value of the turn difference as long as you do it consistently. So if track 1 is the left track and you use track1 minus track2 a positive value indicates a clockwise turn and a negative value an anti-clockwise turn. I think that should work whether the track1 and track2 values are positive or negative.
I'll knock a quick project together to show how to tie the items together.
I'll knock a quick project together to show how to tie the items together.
- deck26
-
1000+ posts
Turning around a single moving point.
Turns out there's a simple method - just offset the costume centres of the tracks. See http://scratch.mit.edu/projects/50649338/
I did manage to work out the trig for one track to follow but was struggling slightly with the second (I think I could do it now) but realised this way would work and is much less work.
I did manage to work out the trig for one track to follow but was struggling slightly with the second (I think I could do it now) but realised this way would work and is much less work.
- gtoal
-
1000+ posts
Turning around a single moving point.
Hello all,
I am trying to create a test rig for a lego robotics project (using brick pi). The robot has tank tracks. I would like to be able to program a sprite for each tank track that means that if one runs forward and one runs backwards the sprites will revolve around a centre point (robot body).
I am sorry if I haven't explained the problem very well!
Any help greatly appreciated!
Tranq
OK, there's two separate requests here. One is animating the tracks, the other is controlling the virtual robot motion.
The robot moves by having each track independently move forward or backward at different speeds.
I've hacked up a quick demo of controlling the tracks here: http://scratch.mit.edu/projects/51548578/
The demo *approximates* the tank motion but doesn't calculate it properly. Straight forward and reverse is easy. Rotations without forward or reverse movement are easy too as they are just around the center of the tank. But to do proper turns while moving forward or back, you need to pivot around a point displaced to one side. Right now I haven't worked out the maths involved but I expect it will involve the separation of the tracks and the speed of each track. I suspect that two concentric arcs will define the pivot point and hence the amount of turn.
You can probably reuse most of the demo code above but replace the turning code using the proper physics/geometry solution once you have it.
meanwhile it gives you something to have a little fun with, learning how to control the tracks independently. (I did this once for real in an armored personnel carrier and it really is quite confusing at first. But great fun!)
Graham
- gtoal
-
1000+ posts
Turning around a single moving point.
As I currently see this being applied the difference between the track speeds would provide the severity of the turn. Therefore, Track A runnign at speed = 5 (forwards) and Track B running at speed = -5 (backwards) would provide a difference of 10 (a tight turn)
Actually, no - if each track has the same speed but opposite sign, it's turning on the spot. The only difference speed makes is how fast it rotates.
where as Track A running at speed = 5 (forwards) and Track B running at speed = 4 (forwards) would still provide a turn with a difference of 1 (a weaker turn). I will need to look at how to work out which direction the turn is in but that should be easy enough.
When the track A is speed 5 and track B is speed 4, then you inscribe a circular arc, turning towards the Track B side. Track A will turn in an arc of length k * 5 and Track B will turn in an arc of length k * 4, and the two arcs will be displaced from each other by the width of the vehicle. This should be enough to find the origin of the circle of which A and B are concentric arcs, which will give you the turning radius. You then apply that to a third arc going through the center of the vehicle to determine its actual motion. It's turn angle will always be tangential to that central arc.
(You can tweak ‘k’ and the vehicle width to come up with values that look reasonable for your sim, unless of course you're trying to do a very accurate simulation with the actual sizes and speeds of some real hardware, in which case you'll need to measure those)

The circumference of a circle is 2 * pi * radius. An arc is just a fraction of a circumference, so the length of an arc is the circumference times angle/360.
You'll have two similar equations for track A and track B, and by defining the angle to be the same for both arcs, you ought to be able to construct an equation for the radius. There's a Khan Academy video that's related though not quite exactly the solution: https://www.khanacademy.org/math/trigonometry/unit-circle-trig-func/radians_tutorial/v/ratio-between-cocentric-arcs
G
Last edited by gtoal (March 10, 2015 14:05:42)
- gtoal
-
1000+ posts
Turning around a single moving point.
PS It wasn't immediately obvious until I thought about it for a little, but when viewed from above, the pattern of the tank tracks will be moving at a speed that is exactly twice that of the actual vehicle. (Actually, twice that of the specific track, if it is turning, and one track is going faster than the other)
ie if travelling in a straight line at 10 pixels per animation, the tracks when viewed from above will be moving at 20 pixels per frame.
That is, if you want it to look realistic. I wouldn't got that far for a video game for example, but it might be worth the effort for a robotics lab sim.
I've hacked in a set of tracks in my demo - it doesn't work them out accurately as suggested above but it does look good enough for a video game…
(btw if anyone is interested in writing a video game with a tank in it - have a look at either Armor Attack or RipOff from the Cinematronic vector arcade games. They would be relatively easy to code in Scratch.)
Graham
ie if travelling in a straight line at 10 pixels per animation, the tracks when viewed from above will be moving at 20 pixels per frame.
That is, if you want it to look realistic. I wouldn't got that far for a video game for example, but it might be worth the effort for a robotics lab sim.
I've hacked in a set of tracks in my demo - it doesn't work them out accurately as suggested above but it does look good enough for a video game…
(btw if anyone is interested in writing a video game with a tank in it - have a look at either Armor Attack or RipOff from the Cinematronic vector arcade games. They would be relatively easy to code in Scratch.)
Graham
Last edited by gtoal (March 10, 2015 20:18:09)
- Discussion Forums
- » Help with Scripts
-
» Turning around a single moving point.