local move_acceleration =100 local max_speed =50 local gravity = -1000 local ground_contact=false -- pre hash for speed local msg_contact_point_response = hash("contact_point_response") local msg_animation_done = hash("animation_done") local group_obstacle = hash("ground") --animations local anim_walk=hash("skeleton_walk") local anim_rise=hash("skeleton_rise") local anim_walk_clothed=hash("skeletonClothed_walk") local anim_rise_clothed=hash("skeletonClothed_rise") local anim_ready=hash("sskeleton_ready") local anim_run=hash("sskeleton_run") local anim_dead=hash("sskeleton_dead") function init(self) self.velocity=vmath.vector3(0,0,0) self.movement=1 self.anim=nil self.move_input = 0 end local function update_animation(self) sprite.set_hflip("#sprite", self.movement>0) end function update(self, dt) local target_speed=self.movement*max_speed local speed_diff = target_speed-self.velocity.x local acceleration = vmath.vector3(0,gravity,0) if (speed_diff~=0) then if(speed_diff<0) then acceleration.x=-move_acceleration else acceleration.x=move_acceleration end end local dv = acceleration * dt if(math.abs(dv.x)>math.abs(speed_diff)) then dv.x = speed_diff end local v0 = self.velocity self.velocity = self.velocity + dv local dp = (v0+self.velocity)*dt update_animation(self) go.set_position(go.get_position() + dp) self.correction=vmath.vector3() self.ground_contact = false self.wall_contact = false; end local function handle_obstacle_contact(self, normal, distance) -- project the correction vector onto the contact normal -- (the correction vector is the 0-vector for the first contact point) local proj = vmath.dot(self.correction, normal) -- calculate the compensation we need to make for this contact point local comp = (distance - proj) * normal -- add it to the correction vector self.correction = self.correction + comp -- apply the compensation to the player character go.set_position(go.get_position() + comp) -- check if the normal points enough up to consider the player standing on the ground -- (0.7 is roughly equal to 45 degrees deviation from pure vertical direction) if normal.y > 0.7 then self.ground_contact = true if math.random() <0.002 then self.movement = self.movement*-1 end end -- project the velocity onto the normal proj = vmath.dot(self.velocity, normal) -- if the projection is negative, it means that some of the velocity points towards the contact point if proj < 0 then -- remove that component in that case self.velocity = self.velocity - proj * normal end end function on_message(self, message_id, message, sender) -- check if we received a contact point message if message_id == msg_contact_point_response then -- check that the object is something we consider an obstacle if message.group == group_obstacle then handle_obstacle_contact(self, message.normal, message.distance) end end if message_id == trigger_response then print("head collision") end end