# Tilemap segment endless generation \[FULLY SOLVED\]

**URL:** <https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705>\
**Category:** Questions\
**Created:** [December 17, 2016, 4:14pm UTC](https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705 "2016-12-17T16:14:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgaronfo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/edgaronfo/32/18438_2.png) [@Edgaronfo](https://forum.defold.com/u/Edgaronfo)\
**Post date:** [December 17, 2016, 4:14pm UTC](https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705/1 "2016-12-17T16:14:50Z")

</div>

Hello every body,

I am trying to implement an endless runner with tilemap segments as suggested by @vigridzki and @britzl in this other thread: [Endless Runner - segment generation](https://forum.defold.com/t/endless-runner-segment-generation/801/4)

I have followed the same setup suggested in the post:

 ![](https://forum-defold.b-cdn.net/uploads/default/original/2X/f/fd061382b4abf1d0f2637a70dc3ad37a7b304aee.png)

The _segment1.collection_ contains the tilemap from @britzl examples

 ![](https://forum-defold.b-cdn.net/uploads/default/original/2X/8/8f3a4f68afa071312df0f85a14c22bbeccca4608.png)  
That tilemap has a tilesource with tiles 64x64 pixels in size (thanks again @sicher for the name of the collection 👌)

The content of _segment.script_ file

```lua
function init(self)
    -- Register segment with the controller
    msg.post("game:/game", "register_segment", { url = msg.url("#collectionfactory"), id = go.get_id() })
end

```

And in the _game.script_ file I have something like this (simplified version):

```lua
function on_message(self, message_id, message, sender)
    if message_id == hash("register_segment") then
        table.insert(self.segments_url, message.url)
        if #self.segments_url == MAX_SEGMENTS then
            self.all_segments_registered = true
            spawn_segment(self)
            spawn_segment(self)
            spawn_segment(self)
        end -- if max segments
    end
end

function spawn_segment(self)
    local xpos = (845 * #self.segments) -- <--------- POSITION IN PIXELS HERE
    local seg_num = 1
    if math.random() > 0.5 then
        seg_num = 2
    end
    -- spawn tilemap
    local segment = collectionfactory.create(self.segments_url[seg_num], vmath.vector3(xpos, 0, 0), nil, { }, 0.6)
    table.insert(self.segments, segment)
end

```

But I can not make the segments spawn together seamlessly, first my logical thought made me get the bounds of the tilemap and multiply them by the size of the tilesource tile

```lua
local x, y, w, h = tilemap.get_bounds(segment_url)
xpos = (x + w) * 64
print("BOUNDS", x, y, w, h, w * 64, xpos)
-- RESULT:
DEBUG:SCRIPT: BOUNDS	-1	1	22	23	1408	1344

```

But the result was a big gap between the tilemaps, then I tried to fine tune it by hand and I arrived to the value **845** shown in the code above, but it only works for the first spawn, after that there is always a small gap of varying dimensions:

 ![](https://forum-defold.b-cdn.net/uploads/default/original/2X/f/ff91acb07f86cc0896499f562bea7dc01f235c9a.png)

I think I might missing some property or some configuration some place but I don’t know where.

Any help, observations or suggestions will be greatly appreciated and welcome.

Thanks!

---

<div class="post-metadata">

**Author:** ![Edgaronfo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/edgaronfo/32/18438_2.png) [@Edgaronfo](https://forum.defold.com/u/Edgaronfo)\
**Post date:** [December 17, 2016, 9:09pm UTC](https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705/2 "2016-12-17T21:09:34Z")

</div>

**UPDATE:**

Ok, so if I remove the code that moves the segment I get rid of the gap:

 ![](https://forum-defold.b-cdn.net/uploads/default/original/2X/b/bdadc920440f13d27c73dfc7fda6702f56bc8896.png)

The position to spawn is tuned by hand,value: **845**

So it seems that the gap is produced by the motion of the segment… 🤔

**EDIT:**

I added the speed of the segment to the spawning position and the gap is gone!

```lua
function spawn_segment(self)
    local last_seg_pos = getLastSegmentPosition()
    xpos = last_seg_pos.x + 845 --<--- SEGMENT SIZE IN PIXELS, TUNED BY HAND
    xpos = xpos - 9 --<--- SEGMENT DISPLACEMENT SPEED
    -- spawn tilemap
    local segment = collectionfactory.create(randomSegmentUrl(), vmath.vector3(xpos, 0, 0), nil, { }, 0.6)
    table.insert(self.segments, segment)
end

```

No need for Tilemap size in pixels. I guess I’ll have to edit the title of this post.

**EDIT 2:**  
I solved my problem, for now, but what happens if I have segments of different sizes? Why didn’t the **tilemap.getbounds.w \* tilesource.tile.width** returned the displacement in pixels?  
(note: ‘tilemap.getbounds.w’ and ‘tilesource.tile.width’ are a simplification, you can’t get the values just like that)

**EDIT 3:**  
Thanks to @Mathias_Westerdahl that pointed me out that the last parameter of the collection factory creation function is _scale_, which in this case is **0.6**.  
See [Collection factory API documentation](http://www.defold.com/ref/collection-factory/)  
(see @Mathias_Westerdahl full answer below)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [December 17, 2016, 10:48pm UTC](https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705/3 "2016-12-17T22:48:22Z")

</div>

The `width * tile_size == 1408` is correct.  
The reason that you use another number is because you spawn the object with smaller scale, `0.6`:

```lua
local segment = collectionfactory.create(self.segments_url[seg_num], vmath.vector3(xpos, 0, 0), nil, { }, 0.6)

```

```python
>>> (22 * 64) * 0.6
844.8

```

Which is pretty close to _845_ 🙂

---

<div class="post-metadata">

**Author:** ![Edgaronfo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/edgaronfo/32/18438_2.png) [@Edgaronfo](https://forum.defold.com/u/Edgaronfo)\
**Post date:** [December 17, 2016, 10:51pm UTC](https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705/4 "2016-12-17T22:51:54Z")

</div>

Ooooooooooooooh… that is why! 🙃

Thank you very, very, very much @Mathias_Westerdahl!

Me so dumb 🙈

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [December 18, 2016, 5:34pm UTC](https://forum.defold.com/t/tilemap-segment-endless-generation-fully-solved/3705/5 "2016-12-18T17:34:05Z")

</div>

Nicely caught @Mathias_Westerdahl! The scale could have easily been overlooked.
