Question

In below mentioned code, what exactly is the difference between sprite, body and ground? Why are there no parameters passes to createBody while declaring ground? what is difference between edgeshape.new() and polygonshape.new()?

require "box2d"

b2.setScale(20)

-- this function creates a box sprite with 2 happy and sad children
local function createBoxSprite(sx, sy)
    local happy = Bitmap.new(Texture.new("happy-box.png", true))
    happy:setAnchorPoint(0.5, 0.5)

    local sad = Bitmap.new(Texture.new("sad-box.png", true))
    sad:setAnchorPoint(0.5, 0.5)

    local sprite = Sprite.new()
    sprite:addChild(happy)
    sprite:addChild(sad)

    sprite:setScale(sx, sy)

    return sprite
end

-- this table holds the dynamic bodies and their sprites
local actors = {}

-- create world
local world = b2.World.new(0, 9.8)

-- create a ground body and attach an edge shape
local ground = world:createBody({})
local shape = b2.EdgeShape.new(-200,480,520,480)
ground:createFixture({shape = shape, density = 0})

-- every 3 seconds, we create a random box
local function onTimer()
    local sx = math.random(70, 100) / 100
    local sy = math.random(70, 100) / 100

    local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = math.random(0, 320), y = -35}}

    local shape = b2.PolygonShape.new()
    -- box images are 70x70 pixels. we create bodies 1 pixel smaller than that.
    shape:setAsBox(34.5 * sx, 34.5 * sy)

  body:createFixture{shape = shape, density = 1, restitution = 0.1, friction = 0.3}

    local sprite = createBoxSprite(sx, sy)
    stage:addChild(sprite)

    actors[body] = sprite
end
Was it helpful?

Solution

Sprite is an empty object to group other Sprite inherited objects as in your case happy and sad Bitmaps.

Body is a representation of the physical body in box2d, it does not have any visual representation, only numbers as dimensions (width and height), position, rotation and different forces. Usually inside ENTER_FRAME event you take this values as position and rotation and apply them to the Sprite inherited object, so it would move exactly like the body in box2d simulated world.

Ground is an empty dummy box2d object. If you don't provide parameters to createBody it means the defaults are used, which basically means you don't care what values it has. The most common example of such body usage is for joints.

To create a joint you usually need two bodies, but what if you would to attach body through joint to some random position in the air, or dynamic position as mouse cursor, well you can do it by creating dummy physics object Here is an example: http://appcodingeasy.com/Gideros-Mobile/Dragging-Box2d-object-in-Gideros-Mobile

In case of your example, then this body is used for EdgeShape which is usually an arbitrary shape used for creating world boundaries or in this case simply ground, so the dynamic object won't fall outside the screen

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top