View Full Version : How to make an object move randomly?

candy

10-09-2002, 10:20 PM

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.

Gavin

10-09-2002, 11:56 PM

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.

MickeyMouse

10-09-2002, 11:57 PM

Quite a newbie.... and OT (this more http://www.opengl.org/discussion_boards/ubb/biggrin.gif) 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!

candy

10-10-2002, 05:44 AM

Thanks Gavin and MickeyMouse, you all are very nice. http://www.opengl.org/discussion_boards/ubb/smile.gif

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 http://www.opengl.org/discussion_boards/ubb/biggrin.gif

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.

SirKnight

10-10-2002, 06:10 AM

normalize(vector) = vector/|vector|

Where | | means magnitude.

-SirKnight

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.