How do I make a kinematic object (player) push a dynamic object?

#1

Like I asked, I want the player to be able to push the object, on contact.

My player.script:

function init(self)
	msg.post(".","acquire_input_focus")
	self.runSpeed = 50
	self.curAnim = "idle"
	msg.post("#idle", "play_animation", { id=hash("idle") })
	self.speed = vmath.vector3()
end

function update(self, dt)
	local pos = go.get_position()
	if self.speed.x ~= 0 then    
		pos = pos + self.speed * dt
		go.set_position(pos)	

		if self.curAnim ~= "run" then
			msg.post("#idle", "play_animation", { id=hash("run") })
			self.curAnim = "run"
		end
	end
end

function on_input(self, action_id, action)
	if action_id == hash("RIGHT") then
		if action.pressed then
			self.speed.x = self.runSpeed
			sprite.set_hflip("#idle", false)
		elseif action.released then
			self.speed.x=0
			msg.post("#idle", "play_animation", { id=hash("idle") })
			self.curAnim = "idle"
			sprite.set_hflip("#idle", false)
		end
	end    
	if action_id == hash("LEFT") then
		if action.pressed then
			self.speed.x = self.runSpeed * -1
			sprite.set_hflip("#idle", true)
		elseif action.released then
			self.speed.x=0
			msg.post("#idle", "play_animation", { id=hash("idle") })
			self.curAnim = "idle"
			sprite.set_hflip("#idle", true)
		end
	end 
end
0 Likes

#2

Depending on what you are after you could do at least 3 ways I can see:

  1. Attach the object ‘go’ to your player ‘go’ through go.set_parent()
  2. Make the object into a dynamic object instead of kinematic
  3. Implement kinematic collisions as described here

For solution 1:
You want some sort of ‘grab’ mechanic, the player can grab and drop the object, and in so doing you will add or remove the player as the parent of the game object. Make sure when the player releases the object to assign the right new parent.

For solution 2:
Let the physics engine handle your collisions, and you can just edit the traits of the object and player.

For solution 3:
Make sure to read the whole page I linked, it goes through handling collisions with kinematic objects well, but to make sure that it handles well will require that you do implement what is described there.

2 Likes

#3

Did you see this post with three different options to achieve what you wanted, complete with a demo project?

3 Likes