algorithm

Associate
Joined
26 Jan 2006
Posts
1,502
Hey guys,

I am trying to develop a random movement algorithm which will do the following:

The object spawns at a random location object.loc.x and objectloc.y and starts moving randomly pixel by pixel in a random direction. However, if it hits a boundary it should not go out of bounds.

for example, if (object.loc.x || object.loc.y == framebound) change direction

I made something based on this kind of if statements but is limited to triangular movements

Thanks for any resources or ideas.
 
Una said:
The language does not matter for the algorithm, pseudo code should be enough.
Fair enough, but thought I could provide some code if I knew the language, as the syntax for generating a random number is language dependant.
 
to get a random direction just choose a random int from 1 - 360 to give you the angle of travel in a 2D plane, now do it again from +90 to -90 (0r (0 - 180) - 90 ) to get the angle from that plane. and then random again for the distance traveled.

Depending on how you wanted to represent it you might prefer to use a different choice of numbers but essentially choose direction, choose distance and apply the transform to your current position..

Paul
 
This might be complete nonsense, but it is the kind of thing I would do for a simple project. I haven't done this kind of coding in a long while.

Code:
//sets the initial position of the object
while _initialPositionBool is not true
{
   _object.position.x = random x axis value within a threshold range*
   _object.position.y = random y axis value within a threshold range*  
   _initialPosition = true
}

//A simple method of producing random movement using 2d vectors
if _ChangeDirectionBool is true //initially set to true in order to provide the first velocity value.
{
   _velocity.x = random float value between -1 and 1 //unit length
   _velocity.y = random float value between -1 and 1 //unit length
   _changeDirectionBool = false
}
else if ChangeDirectionBool is false
{
   //movement - delta time isn't essential depending on complexity
   _object.position =  _objectoldposition + _velocity * _speedmultplier * _deltatick
  
  //timer value - used to mark the period of time between changes of direction
  _timerValue -= timer decrement value * _deltatick
  
  if _timerValue less than or equal to 0 
  {
      _changeDirectionBool = true
      _timerValue set back to orignal value
  }
}

//My preferred method of collision with the extremities of the screen
create line or line segments for each of the 4 edges of the window using the four corner's coordinates

while running
{
   for all the 4 boundaries
   {
      if the object's distance from a line is equal to or less than 0
      {
         collision has occurred
      }
   }
}

//easier method - depends on your coordinate system, but in this case I shall assume 0,0 to be the centre of the screen and the system cartesian
if object.position is equal or greater than screen width / 2 {
  collision has occurred with the right hand side of the window
  change trajectory of object accordingly
  reset collision flag
}
if object.position is equal or greater than screen width / 2 * -1 {
  collision has occurred with the left hand side of the window
  change trajectory of object accordingly
  reset collision flag
}
if object.position is equal or greater than screen height / 2 {
  collision has occurred with the top side of the window
  change trajectory of object accordingly
  reset collision flag
}
if object.position is equal or greater than screen height / 2 * -1 {
  collision has occurred with the bottom side of the window
  change trajectory of object accordingly
  reset collision flag
}

//simple object reaction in the event of boundary collision
invert ordinates of velocity and continue

*typically the size of your window in pixels (in which case, a random integer is fine) or perhaps the defined size of a graphics API viewport

This might be confusing as hell! Pseudo code was never my strong point. It is also worth noting that this will produce quite erratic and angular movement. Coding curving trajectories as a mostly more difficult, so I have tried to keep things simple.

Let me know if anything confounds you, or is plain rubbish.
 
Last edited:
Back
Top Bottom