Game Development Reference
In-Depth Information
87 @object . gun_angle = [ actual - @retarget_speed , desired ]. max
88 end
89 elsif actual < desired
90 if desired - actual > 180 # 360 -> 0 fix
91 @object . gun_angle = ( 360 + actual - @retarget_speed ) % 360
92 if @object . gun_angle > desired
93 @object . gun_angle = desired # damp
94 end
95 else
96 @object . gun_angle = [ actual + @retarget_speed , desired ]. min
97 end
98 end
99 end
100 end
There is some math involved, but it is pretty straightforward. We need to find out an angle
between two points, to know where our gun should point, and the other thing we need is
coordinates of point which is in some distance away from source at given angle. Here are
those functions:
module Utils
# ...
def self . angle_between (x, y, target_x, target_y)
dx = target_x - x
dy = target_y - y
( 180 - Math . atan2(dx, dy) * 180 / Math :: PI ) + 360 % 360
end
def self . point_at_distance (source_x, source_y, angle, distance)
angle = ( 90 - angle) * Math :: PI / 180
x = source_x + Math . cos(angle) * distance
y = source_y - Math . sin(angle) * distance
[ x, y ]
end
# ...
end
Implementing AI Input
At this point our tanks can already defend themselves, even through motion is not yet
implemented. Let's wire everything we have in AiInput class that we had prepared
earlier. We will need a blank TankMotionFSM class with 3 argument initializer and
Search WWH ::




Custom Search