Specify body-shape for a tile as a property?

The tutorial discusses doing it by specifying objects as “points”.

I was looking for a way to specify the body-shape for a tile thru a property with a list of local coordinates, like

bodyShape : [0,-37, 37,-10, 23,34, -23,34, -37,-10]

which would then be passed to the physics.addBody() call if present.

-Frank.

[import]uid: 8093 topic_id: 6863 reply_id: 306863[/import]

Ok - feature was “almost” there - just had to convert shape property values from a string to a lua-table-list:

[lua]local onShapeProperty = function(property, atype, object)
if(type(object.shape)==“string”)then
local shapeT = split(object.shape,",")
if(#shapeT > 1)then
local shape = {}
for i,v in ipairs(shapeT)do table.insert(shape,tonumber(v)) end
object.shape = shape
end
end
end

map:addPropertyListener(“shape”, onShapeProperty)[/lua]

-FS. [import]uid: 8093 topic_id: 6863 reply_id: 23930[/import]

Whoops, I always meant to add this functionality in. I have added the following code to lime-object.lua around line 444 just after the “elseif(self.shape) then” line:

  
if type(self.shape) == "string" then  
  
 local splitShape = splitString(self.shape, ",")  
  
 if #splitShape \> 1 then  
  
 local shape = {}  
  
 for i = 1, #splitShape, 1 do  
 shape[#shape + 1] = tonumber(splitShape[i])  
 end  
  
 body = display.newRect( self.map.world, self.x, self.y, self.width or 1, self.height or 1 )   
  
 self.shape = shape  
 end  
 end   
  

I’ve also added a localised function to the top:

local splitString = utils.splitString [import]uid: 5833 topic_id: 6863 reply_id: 23933[/import]

Will that fix only work for objects and not for tiles?

-FS.
[import]uid: 8093 topic_id: 6863 reply_id: 23938[/import]

Currently the Tiles are only built from their sprite image whereas an Object can be built from a sprite image (if it is a Tile-Object), radius, points, its actual width/height and now a shape. It would be fairly trivial to copy over the code from Object:build() to Tile:build() to get these other options for Tiles if that was wanted? [import]uid: 5833 topic_id: 6863 reply_id: 23941[/import]

It would be great if that feature would also apply to tiles.

I was actually implementing on tiles and not objects to easily build a ramp of tile-images - worked very well.

-FS. [import]uid: 8093 topic_id: 6863 reply_id: 23944[/import]

I will update the Tile:build() function to make it accept other options, *should* be simple enough :slight_smile: [import]uid: 5833 topic_id: 6863 reply_id: 23947[/import]

OK, I have currently just updated the function to allow shapes to be specified, the other options would be trickier. For instance to create a body from points you would also need to specify which layer the point objects are on.

[code]

— Builds the physical representation of the Tile.
function Tile:build()

if(self.HasBody and self.sprite) then

local body = self.sprite

if(self.shape) then

if type(self.shape) == “string” then

local splitShape = splitString(self.shape, “,”)

if #splitShape > 1 then

local shape = {}

for i = 1, #splitShape, 1 do
shape[#shape + 1] = tonumber(splitShape[i])
end

self.shape = shape

end
end
end

– Now that tiles can be set at runtime it is important to make sure physics is loaded as it may not have been at load time
if not physics then
require(“physics”)
physics.start()
end

self.isSensor = stringToBool(self.isSensor)

addCollisionFilterToBody( self )

physics.addBody( body, self )

applyPhysicalParametersToBody(body, self)

addPropertiesToBody(body, self)

end
end

[/code] [import]uid: 5833 topic_id: 6863 reply_id: 23953[/import]

Although if you specify a “radius” property for your tile it should work by default as it just gets past to the physics.addBody function. [import]uid: 5833 topic_id: 6863 reply_id: 23957[/import]