# Horizontal force applied to game object from tile map collision shape

**URL:** <https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520>\
**Category:** Questions\
**Created:** [September 25, 2019, 8:54pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520 "2019-09-25T20:54:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![neet](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@neet](https://forum.defold.com/u/neet)\
**Post date:** [September 25, 2019, 8:54pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/1 "2019-09-25T20:54:13Z")

</div>

It looks like my game object is currently receiving a `contact_point_response` message containing a horizontal contact point (assuming that’s what the debug arrows indicate) while walking along a flat surface, which causes the horizontal movement to be abrupt. The movement is fine while the game object is in the air as it isn’t colliding with any surfaces. Here’s an example screen recording of what is currently happening:

[https://files.catbox.moe/3uhkl3.mp4](https://files.catbox.moe/3uhkl3.mp4)

Mirror: [https://forum-defold-uploads.b-cdn.net/original/3X/e/0/e0b23953077be7058e1499dbd07adf9057f0dfd1.mp4](https://forum-defold-uploads.b-cdn.net/original/3X/e/0/e0b23953077be7058e1499dbd07adf9057f0dfd1.mp4)

Here is what the game object’s update function looks like:

```
function update(self, dt)
	local target_speed = self.move_input * self.max_speed
	local speed_diff = target_speed - self.velocity.x
	local acceleration = vmath.vector3(0, self.gravity, 0)

	if speed_diff ~= 0 then
		if speed_diff < 0 then
			acceleration.x = -self.move_acceleration
		else
			acceleration.x = self.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 * 0.5

	go.set_position(go.get_position() + dp)

	self.correction = vmath.vector3()
	self.move_input = 0
	self.ground_contact = false
end

```

Here’s what the function that handles collision detection looks like:

```
function on_message(self, message_id, message, sender)
	if message_id == msg_contact_point_response then
		if message.group == group_geometry then
			handle_geometry_contact(self, message.normal, message.distance)
		end
	end
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

```

What might be causing this?

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [September 25, 2019, 10:43pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/2 "2019-09-25T22:43:15Z")

</div>

Is your ground made with a tilemap or otherwise with multiple collision shapes? If so then read [this post](https://forum.defold.com/t/directional-platforms-help/18764/31).

Assuming that’s the issue, as far as I can find there is no perfect solution that solves this correctly in all circumstances. The simplest solution is to make your player body rounded or beveled on the bottom. That works well enough if you’re not too picky.

---

<div class="post-metadata">

**Author:** ![sergey.lerg](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sergey.lerg/32/3970_2.png) [@sergey.lerg](https://forum.defold.com/u/sergey.lerg)\
**Post date:** [September 26, 2019, 1:57pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/3 "2019-09-26T13:57:37Z")

</div>

Ideally all collision objects should be made with minimal amount of shapes. So in this case it should be just a single big rectangle.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [September 26, 2019, 2:49pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/4 "2019-09-26T14:49:44Z")

</div>

This may be related to Issue-4152 outlined here:

> [@Sprites tied to physics lag one frame behind (Issue-4152)](https://forum.defold.com/t/sprites-tied-to-physics-lag-one-frame-behind-issue-4152/39859/24):
>
> A few more observations playing with the [PhysicsTunneling.zip](https://forum-defold-uploads.b-cdn.net/original/3X/5/8/58e2558c3dce09d04c4ffec903155ab231500ccd.zip) Defold project: A dynamic collision object with restitution 1 that bounces on a static body with restitution 0 increases each bounce a little. A value of 1 should only retain the bounce height, not increase it. When setting frame cap to 20 the issue becomes much more pronounced, to the point at which the ball tunnels through the other side and disappears. It seems to me this must be related to how Defold updates the physics?

---

<div class="post-metadata">

**Author:** ![neet](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@neet](https://forum.defold.com/u/neet)\
**Post date:** [September 26, 2019, 4:55pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/5 "2019-09-26T16:55:40Z")

</div>

> [@ross.grams](#):
>
> Is your ground made with a tilemap or otherwise with multiple collision shapes? If so then read [this post](https://forum.defold.com/t/directional-platforms-help/18764/31).

I thought it may be an issue with multiple collision shapes, as if I ignore tilesource collisions and just draw a single collision body over the entire tilemap, the collisions work as expected. It appears to draw a single contiguous rectangle over the tile map while debug draw is enabled though so I wasn’t sure.

Using a rounded collision body doesn’t seem to provide as precise collision detection as I would like (the game object “bounces” every time it leaves a tile) so I may try to implement something else or tweak it more. Thanks for the advice.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 26, 2019, 5:51pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/6 "2019-09-26T17:51:24Z")

</div>

This is an annoying problem which has impacted my projects in the past. Tilemap collision shapes need smarter welding of points. It would be nice if we could visualize points with debug on too.

---

<div class="post-metadata">

**Author:** ![neet](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@neet](https://forum.defold.com/u/neet)\
**Post date:** [September 26, 2019, 6:05pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/7 "2019-09-26T18:05:34Z")

</div>

> [@Pkeod](#):
>
> This is an annoying problem which has impacted my projects in the past. Tilemap collision shapes need smarter welding of points. It would be nice if we could visualize points with debug on too.

Which solutions in the past have worked out the best for you while implementing tile map collisions?

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [September 26, 2019, 6:29pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/8 "2019-09-26T18:29:43Z")

</div>

> [@neet](#):
>
> It appears to draw a single contiguous rectangle over the tile map while debug draw is enabled though so I wasn’t sure.

Yeah, the debug drawing doesn’t show the edges, so you can’t really tell, but each tile is a separate shape. You can change tiles however you want at runtime, so this makes sense.

[This reddit thread](https://www.reddit.com/r/gamedev/comments/1w92dm/2d_collision_detection_and_resolution_solving_the/) has a few different suggestions, I haven’t tried to implement them with Defold.

Using raycasts instead of shape collision is probably the the most precise and reliable method you can use with Defold. @dmitriy, have you done this?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 26, 2019, 6:29pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/9 "2019-09-26T18:29:47Z")

</div>

I stopped using tilemaps for collisions.

Also @ross.grams mentioned it but if you use a water tight pill shape it can be better but that has its own drawbacks for certain game types.

Here’s one I use, it is flat on top and rounded on bottom.

[pill.zip](https://forum-defold-uploads.b-cdn.net/original/3X/5/9/59086822cf20903bda5da410b2fa8220416ecdd4.zip) (217 Bytes)

![2019-09-26%2011_35_46-Window](https://forum-defold-uploads.b-cdn.net/original/3X/e/1/e16df3602a8d9a38093c07f45bd9f86b74927146.png)

Instead of adding a shape component you set the shape to this file in the collision.

 ![2019-09-26%2011_36_06-Window](https://forum-defold-uploads.b-cdn.net/original/3X/f/7/f7c19a7b8bc238a57a9c94d68611a37d0c602c50.png)

---

<div class="post-metadata">

**Author:** ![Klear](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/klear/32/11577_2.png) [@Klear](https://forum.defold.com/u/Klear)\
**Post date:** [September 26, 2019, 8:35pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/10 "2019-09-26T20:35:29Z")

</div>

> [@ross.grams](#):
>
> [This reddit thread](https://www.reddit.com/r/gamedev/comments/1w92dm/2d_collision_detection_and_resolution_solving_the/) has a few different suggestions, I haven’t tried to implement them with Defold.

> The typical way (…) can have this done in hours.  
> An intermediate solution (…) have this done in 1 evening.  
> The “correct” solution (…) took me 1 year to get all the details right.

Yikes.

---

<div class="post-metadata">

**Author:** ![dmitriy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dmitriy/32/18452_2.png) [@dmitriy](https://forum.defold.com/u/dmitriy)\
**Post date:** [September 26, 2019, 8:52pm UTC](https://forum.defold.com/t/horizontal-force-applied-to-game-object-from-tile-map-collision-shape/63520/11 "2019-09-26T20:52:03Z")

</div>

> [@ross.grams](#):
>
> have you done this?

More or less.

[https://forum-defold-uploads.b-cdn.net/original/3X/5/e/5eefa4b4aaa5e1bdcd071baa9f6c5334f871f7dd.mov](https://forum-defold-uploads.b-cdn.net/original/3X/5/e/5eefa4b4aaa5e1bdcd071baa9f6c5334f871f7dd.mov)

[Here](https://nsobject.itch.io/test1?secret=FphrZYNFjh2f4RGHUvFO4n8DjdA)(don’t forget to jump to the well) and [here](https://nsobject.itch.io/test2?secret=XvWjpLMFWYKidGemXXstO2bsyI) some sort of old unfinished short demos. You can try them if you want to “feel” how ray casts works. They are still implemented with async raycast, so no slopes in them.
