2
\$\begingroup\$

I have a paper airplane that I would like to take a "random" path towards its destination based on the game layouts size. I want it to include spirals and loops and gliding a little back and forth.

How would I go on about creating those random movements?

enter image description here

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I assume you know the basic maths stuff concerning angles, movement, distance and all the other stuff.

You have these goals:

  • reach destination
  • do random stuff
  • stay inside (rectangular?) area of playfield

Your plane could have these properties:

  • position pos
  • forward-speed vel
  • forward-acceleration acc
  • heading/rotation in degrees rot
  • angular velocity (rotation per second) rotVel
  • angular acceleration (change of rotation per second) rotAcc

Of which pos, vel and rot are chosen when spawning the plane and then never touched again by the random movement code and the others are used to steer it (randomly) towards it's target.

To move the plane, this is getting executed each tick (delta is the time between two updates):

vel += acc*delta;
pos += vel*unitVectorOfAngle(rot)*delta;
rotVel += rotAcc*delta;
rot += rotVel*delta;

So what you could do with this:

1. Loops and turns

You set rotAcc to a random positive or negative value, and leave it like that until you reach a randomly chosen Angle (or for a randomly chosen time or until you are heading towards a randomly chosen point, whatever works best) and then set rotAcc = -rotAcc to smoothly decrease your turnrate until rotVel is zero again. In the image, two example-paths are shown, red is the acceleration phase, green the deceleration phase.


loops and turns

2. Gliding back and forth

You set acc to a relatively big negative value and decelerate until you move back fast enough. Then you wait for a random amount of time and set acc to a high positive value until you reach your original speed again, then set it to zero.

3. Reaching your destination

You should constrain the randomness of your planes movement based on the time that has elapsed since it was started and the distance to your goal. just initiate the random moves less frequently and try to correct its path towards the target, by tweaking rotAcc.

4. Constraining the fly area

For countering situations in which the plane goes to near to a border, you could use a combination of gliding back and doing a fast loop or a manual turn.

5. Mix it up and time it

I can't help you there, you will need to figure out all the tweaking and timing out for yourself.

\$\endgroup\$
1
\$\begingroup\$

The most simple solution must be to randomize path points. Each point can only be within a certain angle from the previous ones. After x points have been created, change the angle setting to make it steer towards the destination.

Something like this:

Vector2 currPos = plane.pos;
Vector2 currDir = plane.startDir;

for(int num = 0; num < MaxPoints; num++){
   double angle;
   if(num < SteerToExit){
    angle = Random(-HalfMaxAngle, HalfMaxAngle);
   }
   else
   {
   Vector2 vToExit = ExitPos - currPos;
   double exitAngle = AngleBetweenVectors(vToExit, currDir);
    //You could add some check here and make sure angle stays 
    //within +-HalfMaxAngle. Otherwise the plane might do a 180 turn at this point. 
    //If it's outside, just make the loop run a few more times
   angle = exitAngle;
 }

   Vector2 newDir = Vector2.Transform(currDir, Matrix.CreateRotationZ(angle));
   newDir = Vector2.Normalize(newDir) * Step;
   currPos = currPos + newDir;
   path.Add(currPos);
   currDir = newDir;

   if((currPos - ExitPos).Length() < CertainValue)
     num = MaxPoints; //Yay we found the destination!
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .