# How to make an object move randomly?

I am now making a fish swimming in the 3D world.

How can I make it swims to specific position which the position is generated randomly?

How can I make the fish rotate itself and face to that position?

Thanks you.

Well you make random poinmts using a random number generator. Then give your fish rotational and linear acceleration traits. Use these to interpolate the per frame movement.

Quite a newbie… and OT (this more ) question.

I suppose your fish has something like position and angles attached to it…

At start you can do something like:

step = 1;
pos = (0, 0, 0);
desired_pos = pos;

while (1)
{
if (distance(pos, desired_pos) < step)
{
desired_pos = (rand() % 100, rand() % 100, rand() % 100)
}

pos = pos + normalize(desired_pos - pos) * step;

render_fish()

}

Next step is to take care of fish angles.
Good luck!

Thanks Gavin and MickeyMouse, you all are very nice. But I still have some confusion…

1. How to calculate “distance(pos, desired_pos)”, is that I should use the desired_pos vector - pos vector?

2. What is the mathematical meaning of “normalize(desired_pos - pos)”?

3. Please give me some more ideas on how to deal with angles.

Thx Thx Thx well distance between 2 points A(x1,y1,z1) and B(x2,y2,z2) is :

Distance = sqrt( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2).

and normalising a vector means you convert the vectoe V(12i,6j,0k) into a unit vector (where all components add up to 1)

V(0.66i,0.33j,0k)

sorry but i cant remember the equation off hand just now.

normalize(vector) = vector/|vector|

Where | | means magnitude.

-SirKnight