//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