# Make an object fall from map's bounds

**URL:** <https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903>\
**Category:** Million Tile Engine\
**Created:** [April 24, 2014, 5:35pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903 "2014-04-24T17:35:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 24, 2014, 5:35pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/1 "2014-04-24T17:35:20Z")

</div>

Hello, I would like the player to be able to get out of the map, for example when he dies. Is there any way to do that ? Also, for the moment I’m using sensor tiles all around the map’s bounds to detect if the player falls out from map (collision). Is there another way to check if an object is within the map’s bounds ?

[![mini_140424074317146463.png](http://nsa34.casimages.com/img/2014/04/24/mini_140424074317146463.png)](http://www.casimages.com/img.php?i=140424074317146463.png)

Thanks you very much,

Ulysse

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 6:09pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/2 "2014-04-25T18:09:57Z")

</div>

Hello ulydev5,

Sprites are constrained to the map by default. You can set map constraints for each edge of the map individually by setting a sprite’s constrainToMap property, which takes a table of booleans.

```
--sprite.constrainToMap = {left, top, right, bottom} sprite.constrainToMap = {false, false, false, false}

```

You can also set this in a sprite’s setup table before calling addSprite. The table parameter is formatted the same way as the sprite’s property and has the effect of setting the sprite’s property just as above.

Checking whether an object is in the maps bounds is a matter of retrieving the size of the map and checking whether the sprite’s .x and .y properties are greater than 0 and less than the map size. If any one value is outside of this area, the map is outside of the map bounds. You may want to add the sprite’s width and height to these checks to determine when the entire sprite has left the map rather than just its origin.

```
local map = mte.getMap() local mapWidth = map.width \* map.tilewidth local mapHeight = map.height \* map.tileheight local gameLoop = function(event) if sprite.x \< 0 or sprite.x \> mapWidth or sprite.y \< 0 or sprite.y \> mapHeight then print("The sprite has left the map") end ... mte.update() end Runtime:addEventListener("enterFrame", gameLoop)
```

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 25, 2014, 7:21pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/3 "2014-04-25T19:21:13Z")

</div>

Hello dyson, thanks you for the reply, unfortunately “player.constrainToMap = {true,true,true,false}” doesn’t work for me. I even tried to put it in the setup table for addSprite but it doesn’t work either. The player keeps getting stuck when reaching the bottom of the map…

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 7:32pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/4 "2014-04-25T19:32:30Z")

</div>

Can you set&nbsp;physics.setDrawMode(“hybrid”) after physics.start() and check whether the player’s physics body immediately turns grey or dark yellow when it gets stuck? If so one thing to try is setting the player’s offscreenPhysics parameter to true in the setup table.&nbsp;

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 25, 2014, 8:13pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/5 "2014-04-25T20:13:33Z")

</div>

It immediately turns grey when it gets stuck. So I did what you said, setting the player’s offscreenPhysics parameter to true, and now there’s an even stranger issue : it gets stuck a few pixels lower than without offscreenPhysics. Plus, it doesn’t turn gray, it stays orange.

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 8:26pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/6 "2014-04-25T20:26:51Z")

</div>

Alright, new plan. Remove the offscreenPhysics parameter, go to line 9910, and comment out this block of code:

```
local locX = math.ceil(object.x / map.tilewidth) local locY = math.ceil(object.y / map.tilewidth) if (locX \< masterGroup[i].vars.camera[1] - 1 or locX \> masterGroup[i].vars.camera[3] + 1) or (locY \< masterGroup[i].vars.camera[2] - 1 or locY \> masterGroup[i].vars.camera[4] + 1) then object.isBodyActive = false elseif (locX \< masterGroup[i].vars.camera[1] or locX \> masterGroup[i].vars.camera[3]) or (locY \< masterGroup[i].vars.camera[2] or locY \> masterGroup[i].vars.camera[4]) then object.isAwake = false object.isBodyActive = true else object.isAwake = true object.isBodyActive = true end
```

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 25, 2014, 8:42pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/7 "2014-04-25T20:42:43Z")

</div>

Still not able to leave the screen. The player is still active (orange rectangle), though.

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 9:16pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/8 "2014-04-25T21:16:06Z")

</div>

At this point the quickest way for me to debug the problem would be to have it in front of me. If you’re willing to email the project to me I’ll have a look at it for you, try to figure out why its refusing to cooperate.

---

<div class="post-metadata">

**Author:** ![Gremlin\_Interactive](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/b5ac83/32.png) [@Gremlin\_Interactive](https://forums.solar2d.com/u/Gremlin_Interactive)\
**Post date:** [April 25, 2014, 10:07pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/9 "2014-04-25T22:07:21Z")

</div>

Could you not just do:

```
physics.removeBody( player )

```

When the player dies?

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 28, 2014, 2:54pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/10 "2014-04-28T14:54:07Z")

</div>

I received your project, but it was in the form of a built application. I need the raw project folder containing your lua code and assets so that I can open them in Simulator and my code editor.

Also I’m just going to throw this out here to cover all possible bases, but the sensor tiles you’re using to detect when a player leaves the map- they aren’t solid physics objects are they? The player isn’t falling to the edge of the map and landing on them?&nbsp;

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 28, 2014, 3:38pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/11 "2014-04-28T15:38:37Z")

</div>

dyson solved the problem. Just had to set the contrainToMap property AFTER calling AddSprite. Thanks you very much, dyson. I should have tried that before asking you.

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 6:09pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/12 "2014-04-25T18:09:57Z")

</div>

Hello ulydev5,

Sprites are constrained to the map by default. You can set map constraints for each edge of the map individually by setting a sprite’s constrainToMap property, which takes a table of booleans.

```
--sprite.constrainToMap = {left, top, right, bottom} sprite.constrainToMap = {false, false, false, false}

```

You can also set this in a sprite’s setup table before calling addSprite. The table parameter is formatted the same way as the sprite’s property and has the effect of setting the sprite’s property just as above.

Checking whether an object is in the maps bounds is a matter of retrieving the size of the map and checking whether the sprite’s .x and .y properties are greater than 0 and less than the map size. If any one value is outside of this area, the map is outside of the map bounds. You may want to add the sprite’s width and height to these checks to determine when the entire sprite has left the map rather than just its origin.

```
local map = mte.getMap() local mapWidth = map.width \* map.tilewidth local mapHeight = map.height \* map.tileheight local gameLoop = function(event) if sprite.x \< 0 or sprite.x \> mapWidth or sprite.y \< 0 or sprite.y \> mapHeight then print("The sprite has left the map") end ... mte.update() end Runtime:addEventListener("enterFrame", gameLoop)
```

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 25, 2014, 7:21pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/13 "2014-04-25T19:21:13Z")

</div>

Hello dyson, thanks you for the reply, unfortunately “player.constrainToMap = {true,true,true,false}” doesn’t work for me. I even tried to put it in the setup table for addSprite but it doesn’t work either. The player keeps getting stuck when reaching the bottom of the map…

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 7:32pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/14 "2014-04-25T19:32:30Z")

</div>

Can you set&nbsp;physics.setDrawMode(“hybrid”) after physics.start() and check whether the player’s physics body immediately turns grey or dark yellow when it gets stuck? If so one thing to try is setting the player’s offscreenPhysics parameter to true in the setup table.&nbsp;

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 25, 2014, 8:13pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/15 "2014-04-25T20:13:33Z")

</div>

It immediately turns grey when it gets stuck. So I did what you said, setting the player’s offscreenPhysics parameter to true, and now there’s an even stranger issue : it gets stuck a few pixels lower than without offscreenPhysics. Plus, it doesn’t turn gray, it stays orange.

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 8:26pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/16 "2014-04-25T20:26:51Z")

</div>

Alright, new plan. Remove the offscreenPhysics parameter, go to line 9910, and comment out this block of code:

```
local locX = math.ceil(object.x / map.tilewidth) local locY = math.ceil(object.y / map.tilewidth) if (locX \< masterGroup[i].vars.camera[1] - 1 or locX \> masterGroup[i].vars.camera[3] + 1) or (locY \< masterGroup[i].vars.camera[2] - 1 or locY \> masterGroup[i].vars.camera[4] + 1) then object.isBodyActive = false elseif (locX \< masterGroup[i].vars.camera[1] or locX \> masterGroup[i].vars.camera[3]) or (locY \< masterGroup[i].vars.camera[2] or locY \> masterGroup[i].vars.camera[4]) then object.isAwake = false object.isBodyActive = true else object.isAwake = true object.isBodyActive = true end
```

---

<div class="post-metadata">

**Author:** ![ulydev5](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/u/f0a364/32.png) [@ulydev5](https://forums.solar2d.com/u/ulydev5)\
**Post date:** [April 25, 2014, 8:42pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/17 "2014-04-25T20:42:43Z")

</div>

Still not able to leave the screen. The player is still active (orange rectangle), though.

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 25, 2014, 9:16pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/18 "2014-04-25T21:16:06Z")

</div>

At this point the quickest way for me to debug the problem would be to have it in front of me. If you’re willing to email the project to me I’ll have a look at it for you, try to figure out why its refusing to cooperate.

---

<div class="post-metadata">

**Author:** ![Gremlin\_Interactive](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/g/b5ac83/32.png) [@Gremlin\_Interactive](https://forums.solar2d.com/u/Gremlin_Interactive)\
**Post date:** [April 25, 2014, 10:07pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/19 "2014-04-25T22:07:21Z")

</div>

Could you not just do:

```
physics.removeBody( player )

```

When the player dies?

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 28, 2014, 2:54pm UTC](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903/20 "2014-04-28T14:54:07Z")

</div>

I received your project, but it was in the form of a built application. I need the raw project folder containing your lua code and assets so that I can open them in Simulator and my code editor.

Also I’m just going to throw this out here to cover all possible bases, but the sensor tiles you’re using to detect when a player leaves the map- they aren’t solid physics objects are they? The player isn’t falling to the edge of the map and landing on them?&nbsp;

[Next page](https://forums.solar2d.com/t/make-an-object-fall-from-maps-bounds/324903.md?page=2)
