Simple collision issue

Hello, I’m having trouble with making a simple wall collision. I have my kinematic object player object, and my static wall object. I’ve read all sorts of answers to collision questions, but somehow nothing is working. I’ve read all the official collision info on the defold site, but I can’t figure this out.


my setup


script for the wall object


script for player object

You have put the on_message() function inside the on_input() function.

I fixed that, but it didn’t make a difference. I still go straight through the other objects.


The nightstand object is static, but it has a sprite and you can see whenever I touch it, it shoots away in whatever direction I touch it. If I keep moving, it will shoot right back to a different place on the screen. I just want to touch it, and be forced to stop.

No, you haven’t fixed it!

Look at your on_input function. Is there an end to close it? Look at your on_message function. Is there one end to close the conditional, and one to close the on_message function itself?

Please post code as text in the future. It would make it easier for us to help you.

Use ``` on either side of the code to format nicely.

```
local foo = “bar”
```

Becomes:

local foo = "bar"
1 Like

Thank you for the information, I’m learning as I go. :slight_smile:

So I went back and added the ends you pointed out, but adding one or both now makes defold refuse to run a build. I’m not sure what you mean by using ` to format nicely, I’m afraid. :sweat_smile:

function init(self)
msg.post(".", “acquire_input_focus”)
msg.post("@render:", “use_fixed_fit_projection”, { near = -1, far = 1 })
self.vel = vmath.vector3() –
end

function update(self, dt)
local pos = go.get_position() –
pos = pos + self.vel * dt –
go.set_position(pos) –

self.vel.x = 0 --
self.vel.y = 0

end

function on_input(self, action_id, action)
if action_id == hash(“up”) then
self.vel.y = 150 –
elseif action_id == hash(“down”) then
self.vel.y = -150
elseif action_id == hash(“left”) then
self.vel.x = -150 –
elseif action_id == hash(“right”) then
self.vel.x = 150
end
end

function on_message(self, message_id, message, sender)
if message_id == hash(“contact_point_response”) then
go.set_position(go.get_position() + message.normal * message.distance)
end
end
end

end

Thank you for your patience.

Wrap your code in three ` on either side of it, like in my example above.

Basically, what we’re saying is that one end is in the wrong place, so adding more will not help.

Oooooh, right. I get what you mean about formatting. Thank you! Is this correct now? It will build, but still same behavior as the video showed.

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("@render:", "use_fixed_fit_projection", { near = -1, far = 1 })
	self.vel = vmath.vector3() --
end

function update(self, dt)
	local pos = go.get_position() -- 
	pos = pos + self.vel * dt --
	go.set_position(pos) --

	self.vel.x = 0 --
	self.vel.y = 0
end

function on_input(self, action_id, action)
	if action_id == hash("up") then
		self.vel.y = 150 -- 
	elseif action_id == hash("down") then
		self.vel.y = -150
	elseif action_id == hash("left") then
		self.vel.x = -150 -- 
	elseif action_id == hash("right") then
		self.vel.x = 150
end


function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		go.set_position(go.get_position() + message.normal * message.distance)
	end
end

	
end
1 Like

Formatting is great now!

Try this. Compare the end placements. Every conditional (if statement) needs an end to close it. Every function (on_input and on_message) needs an end to close it.

function on_input(self, action_id, action)
	if action_id == hash("up") then
		self.vel.y = 150
	elseif action_id == hash("down") then
		self.vel.y = -150
	elseif action_id == hash("left") then
		self.vel.x = -150
	elseif action_id == hash("right") then
		self.vel.x = 150
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		go.set_position(go.get_position() + message.normal * message.distance)
	end
end
1 Like

It was my very very last ‘end’ that was uneeded and stopped the build, so when I got rid of that, what you showed works. Sort of. :joy: That poor little nightstand sprite…

Thank you for taking the time to help me, I spent hours trying to solve this on my own as a newbie!

1 Like