Game Development Reference
In-Depth Information
Problem 2: Get Your Inside Out
We also face another problem as a result of discrete time sampling in collision detection (see
Figure 2-15). Even if two instances are not colliding at all in one step (a), they can still have a large
overlap by the time the collision event is triggered in the next step (b). The collision event may
stop Fishpod moving, but if the instances remain overlapping in this way, then the collision event
will continue to get triggered in every step. Therefore, we need to make sure that after instances
collide, they finish up touching—but not overlapping. Unfortunately, we only know about the
collision after it happens, so putting it right means going back in time to the previous step to work
out the exact point at which the collision should have occurred (c). You can then move the sprites
toward each other one pixel at a time until they reach the precise point of collision (d). Don't
worry—there is an action in Game Maker to do this for you, but it will help a lot if you can
understand the approach.
Figure 2-15. Discrete time sampling means that instances may be inside each other when they collide (left), so
we need to move them back to their position in the previous step and lower them to the contact point (right)
Walk On
Now that we know a bit more about what we need to achieve with the collision events, we can
start to handle the remaining state transitions for our walking state object. This needs to include
an event for handling (sideways) collisions with platforms, as well as a way of detecting whether
the character is supported by a platform or not (see Figure 2-9). We'll add the collision event first,
which—as discussed previously—needs to go back in time to the previous step and then position
Fishpod at the point of collision with the platform.
Going back in time is not actually as impossible as it sounds. You may recall that every object
has x and y variables that store the position of an instance in the room. Well, every object also has
xprevious and yprevious variables that store the previous position of the instance in the room (in
the previous step). By setting x to xprevious and y to yprevious , we can effectively move the
instance back in time to the step before the collision happened. You can then find the exact
collision point using the Move to Contact action. This action moves the instance in a given
direction, one pixel at a time, until it collides with something. All objects also have a direction
variable, which indicates an instance's current movement direction, so we can use this in the
Move to Contact action to make sure we get the contact point with a colliding platform. Putting
all these things together we can create our collision event.
 
Search WWH ::




Custom Search