Moveable Objects Program
description
Transcript of Moveable Objects Program
Moveable Objects Program
Creating objects that know where they’re going
Moveable Objects
• This program demonstrates how to create objects which know where they’re going and have some idea of how to get there.
• Allow me to demonstrate:
So what happens when I click a button?
• All that changes is that the Destination property of the object called “Cust” is changed.
• So how does Cust start moving towards the new destination? Well, there’s a timer:
• Obviously, there’s some serious encapsulation going on here!
A Simple Motion Algorithm
• For this program, I used a simple model for the motion of the objects:– Each object has a MaxSpeed property, which is really the
maximum number of pixels it can move in a tick of the timer.
– If the object is farther than MaxSpeed away from its Destination (property), it will move MaxSpeed pixels towards the Destination.
– If it is less than or equal to MaxSpeed away, it will move directly to Destination and remain there until Destination is changed.
Doing the Math
The Algorithm in Code
• Calling this procedure for every object every time in the Timer_Tick sub will cause all objects to move smoothly towards their destinations.
The Stalker
• The Stalker works because its Destination is changed to the other object’s location every tick of the clock: