行動 (Movement) 靜宜大學資工系 蔡奇偉 副教授
大綱 The Basics of Movement Algorithms Kinematic Movement Algorithms Steering Behaviors Combining Steering Behaviors Predicting Physics Jumping Coordinated Movement
遊戲人工智慧的模型
The Movement algorithm Structure
Statics struct Static: position # a 2D vector orientation # a single floating point value The 2D movement axes and the 3D basis The positions of characters in the level
右手座標系統: 左手座標系統:
Kinematics struct Kinematic position # a 2 or 3D vector orientation # a single floating point value velocity # another 2 or 3D vector rotation # a single floating point value struct SteeringOutput: linear # 加速度 a 2 or 3D vector angular # 角加速度 a single floating point value
struct Kinematic: ... Member data as before ... def update(steering, time): # Update the position and orientation position += velocity * time + 0.5 * steering.linear * time * time orientation += rotation * time + 0.5 * steering.angular * time * time # and the velocity and rotation velocity += steering.linear * time rotation += steering.angular * time
簡化版(因為 time 值很小) struct Kinematic: ... Member data as before ... def update (steering, time): # Update the position and orientation position += velocity * time orientation += rotation * time # and the velocity and rotation velocity += steering.linear * time rotation += steering.angular * time
Kinematic Movement Algorithms Seek Flee Arriving Wandering
def getNewOrientation(currentOrientation, velocity): # Make sure we have a velocity if velocity.length() > 0: # Calculate orientation using an arc tangent of # the velocity components. return atan2(-velocity.x, velocity.z) # Otherwise use the current orientation else: return currentOrientation
Kinematic Seek(尋找) A kinematic seek behavior takes as input the character’s and their target’s static data. It calculates the direction from the character to the target and requests a velocity along this line. target character
struct KinematicSteeringOutput: velocity rotation class KinematicSeek: # Holds the static data for the character and target character target # Holds the maximum speed the character can travel maxSpeed
# Create the structure for output target character def getSteering(): # Create the structure for output steering = new KinematicSteeringOutput() # Get the direction to the target steering.velocity = target.position - character.position # The velocity is along this direction, at full speed steering.velocity.normalize() steering.velocity *= maxSpeed # Face in the direction we want to move character.orientation = getNewOrientation(character.orientation,steering.velocity) # Output the steering steering.rotation = 0 return steering
Kinematic Flee(逃跑) If we want the character to run away from their target, we can simply reverse the second line of the getSteering method to give # Get the direction away from the target steering.velocity = character.position - target.position The character will then move at maximum velocity in the opposite direction. target character
Arriving(抵達) (1) (2) (3) (4) 超過目標,須返回 target character target
解決方法: 一、角色進入目標範圍圈內,即表示已抵達。 二、減速接近目標。 三、在目標範圍圈才減速接近目標。 target character
Wandering(徘徊) A kinematic wander behavior always moves in the direction of the character’s current orientation with maximum speed. The steering behavior modifies the character’s orientation, which allows the character to meander as it moves forward.
class KinematicWander: character maxSpeed # Holds the maximum rotation speed we’d like, probably # should be smaller than the maximum possible, to allow # a leisurely change in direction maxRotation def getSteering(): # Create the structure for output steering = new KinematicSteeringOutput() # Get velocity from the vector form of the orientation steering.velocity = maxSpeed * character.orientation.asVector() # Change our orientation randomly steering.rotation = randomBinomial() * maxRotation # Output the steering return steering def randomBinomial(): return random() - random()
Steering Behaviors