# Help with angular velocity (2d - Flash)

This topic is 3994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I started an angular snake game (see here) and, with the help of an excellent Flash/trigonometry book, I got it working pretty quickly. However trigonometry is new to me and I'm having some difficulty controlling the speed. The snake is made up of an array of circular Movieclips. I loop through them, starting at the tail, and move each of them toward the MC in front of it. The head MC looks at it's rotation (controled by L/R arrow keys) and moves itself in that direction. Below is the relevant code snippet.
var speed = 15;

// loop through snake MC's
for(var i=snakeCells.length; i>=0; i--)
{
if(i==0)	// Move first MC ('Head')
{
var radians:Number = snakeCells[0]._rotation * Math.PI / 180;

// radians to X, Y speed
var ax:Number = Math.cos(radians) * speed;
var ay:Number = Math.sin(radians) * speed;

// move MC
snakeCells[0]._x -= ax;
snakeCells[0]._y -= ay;
}
else            // Move following MC's ('body')
{
// calculate distance & angle to leading MC
var dx:Number = snakeCells[i-1]._x - snakeCells._x;
var dy:Number = snakeCells[i-1]._y - snakeCells._y;
var angle:Number = Math.atan2(dy, dx);

// set angle and move MC
snakeCells._rotation = angle * 180 / Math.PI;
var vx:Number = Math.cos(angle) * speed;
var vy:Number = Math.sin(angle) * speed;
snakeCells._x += vx;
snakeCells._y += vy;
}
}


There is no problem when the speed variable is equal to the MC diameter, but once it goes up, spaces appear between the MC's, likewise when it goes down, the MC's start to overlap. Although I understand most of it, it seems I'm not yet familiar enough with the trig formula to recognise the problem. It seems like my speed variable isn't really the speed? Can anybody tell what I'm doing wrong? [Edited by - Kirl on January 6, 2008 10:38:31 AM]

##### Share on other sites
As far as i can see the problem is like this....

I drew a picture (in MS Paint, so excuse the bad quality) of what you are doing...

The 4 green MCs show the orignal position. The snake is moving right, and its turning counterclockwise.

Now the blue line is the direction of the head moving, and the red circle is the new position of the head, at a distance of 'speed' units.

Now when you find the distance between the 2nd MC and the head, you are taking the direction of the new position of the head with the old position of the 2nd MC, which makes the direction line steeper (black arrow), and then you move in that direction a length of 'speed' units (outlined circle). The direction for the 3rd MC is even steeper (pink arrow) and so on.

Obviously, the new distance between the head and the 2nd MC is not equal the distance between the two pieces as it was before, and hence the gap/overlap. Only if 'speed' is equal to the diameter of the MC, the distance travelled will still be the same.

I can see that the two constraints will be (1) distance travelled by each MC has to be equal to 'speed' and (2) distance between two MCs must be constant. Try to use these constraints to calculate the new position and orientation of the subsequent MCs, and not a direct distance measurement.

Solution:
Let A be the head and B be the 2nd MC,
Let A0,B0 betheir original positions and A1,B1 be their final positions.
Equations:
d(A1,B1) = k = d(A0,B0) ... this is the constant gap between any 2 MCs.
d(B0,B1) = speed .... as the total distance convered has to be equal to 'speed'.

Since we know A0,B0,A1,x,speed.... B1 can be calculated. Same goes for all subsequent MCs.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013393
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!