Falling off platform with collision (SOLVED)

My character is falling off a platform(I followed the runner
the “controller” collision type is static, group: geometry, Mask: player
“player” collision type is kinematic, group: player, mask: geometry
tried to remake it again still falling

local gravity = -20
local jump_takeoff_speed = 900
local jumping = false

function init(self)
	msg.post(".", "acquire_input_focus")
	self.position = go.get_position()
	self.velocity = vmath.vector3(0, 0, 0)
	self.ground_contact = false
end

function final(self)
	msg.post(".", "release_input_focus")
end

function update(self, dt)
	local gravity = vmath.vector3(0, gravity, 0)

	if not self.ground_contact then
		self.velocity = self.velocity + gravity
	end
	go.set_position(go.get_position() + self.velocity * dt)

	self.correction = vmath.vector3()
	self.ground_contact = false

	if self.teleport == true then
		self.position = vmath.vector3(150,self.position.y,self.position.z)
		go.set_position(self.position)
	end
	self.teleport = false
end

local function handle_geometry_contact(self, normal, distance)
	local proj = vmath.dot(self.correction, normal)
	local comp = (distance - proj) * normal
	self.correction = self.correction + comp
	go.set_position(go.get_position() + comp)
	if normal.y > 0.7 then
		self.ground_contact = true
	end
	proj = vmath.dot(self.velocity, normal)
	if proj < 0 then
		self.velocity = self.velocity - proj * normal
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		-- check if we received a contact point message. One message for each contact point
		if message.group == hash("geometry") then
			handle_geometry_contact(self, message.normal, message.distance)
		end
	end
end

local function jump(self)
	if self.ground_contact then
		self.velocity.y = jump_takeoff_speed
		jumping = false
	end
end

function on_input(self, action_id, action)
	if action_id == hash("jump") then
		jump(self)
		if jumping == false then
			msg.post("#sound", "play_sound")
			jumping = true
		end
	elseif action_id == hash("teleport") then
		self.teleport = true
	end
end

Hard to tell what’s wrong. Can you double check that all scripts are added? Also double check collision groups and masks. Another thing to try is to set a breakpoint on the first line of the on_message function and see if it gets called at all.

You can also compare to the complete source code here: https://github.com/defold/defold-examples/tree/master/runner

Last, send the project to me (bjorn.ritzl@gmail.com) and I can take a look.

Alright thanks man Ill look at it and give you an answer in like 31 hours :slight_smile:

1 Like

how do I set a breakpoint in lua? tried the ways like in c++ c# didnt work

You can place a breakpoint by clicking to the right of the line number in the code editor gutter or via the menu: Debug > Break.

More information here: https://www.defold.com/manuals/debugging/#_running_the_debugger

1 Like

tried

You can place a breakpoint by clicking to the right of the line number in the code editor

didnt really put a breakpoint

Like this:

breakpoint

2 Likes

alright did that for on_message function and its not responding(the “game”)

it is called as far as I just tried with the breakpoint but its still falling

Please zip the project (exclude build and .git folder) and send it to me (bjorn.ritzl@king.com) and I’ll take a look.

1 Like

sent btw

Ok, got it. This is what I found.

I started by enabling Physics Debug in game.project. This allows me to see the collision shapes:

There’s a tiny collision shape for the ground. No surprise the character is falling through the ground.

Let’s take a look at the collection in the editor:

That looks better, but the game object is scaled and the runtime shape doesn’t look like in the editor. The explanation can be seen when we look at the shape itself:

The shape is 20x20 pixels, which corresponds to what we see at runtime. The size of collision shapes aren’t scaled with the game object scale (there’s a ticket for this, but it’s not at the top of our list). You should set the size of the collision shape directly on the shape.

@Erik_Angelin and @mats.gisselson: We need to make sure that the editor doesn’t scale the shapes. I’ll create a ticket.

Here: https://github.com/defold/editor2-issues/issues/2138

4 Likes

Is there a way as the engine’s user to prevent that?

No, currently no way to prevent that, but the issue I linked should make sure that the visualisation in the editor matches the behaviour of the engine.

alright man :slight_smile: thanks im very thankful for this engine :ok_hand:

1 Like