Game Development Reference
In-Depth Information
Debugging Bullet Physics
When playing the game, there is a feeling that bullets start out slow when fired and
gain speed as time goes. Let's review BulletPhysics#update and think why this is
happening:
class BulletPhysics < Component
# ...
def update
fly_speed = Utils . adjust_speed(object . speed)
fly_distance = ( Gosu . milliseconds - object . fired_at) *
0.001 * fly_speed / 2
object . x, object . y = point_at_distance(fly_distance)
check_hit
object . explode if arrived?
end
# ...
end
Flaw here is very obvious. Gosu.milliseconds - object.fired_at will
be increasingly bigger as time goes, thus increasing fly_distance . The fix is
straightforward - we want to calculate fly_distance using time passed between calls
to BulletPhysics#update , like this:
class BulletPhysics < Component
# ...
def update
fly_speed = Utils . adjust_speed(object . speed)
now = Gosu . milliseconds
@last_update ||= object . fired_at
fly_distance = (now - @last_update ) * 0.001 * fly_speed
object . x, object . y = point_at_distance(fly_distance)
@last_update = now
check_hit
object . explode if arrived?
end
# ...
end
But if you would run the game now, bullets would fly so slow, that you would feel like Neo
in The Matrix. To fix that, we will have to tell our tank to fire bullets a little faster.
class Tank < GameObject
# ...
Search WWH ::




Custom Search