Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Collision detection — Gideros Forum

Collision detection

VeliasVelias Member
edited January 2013 in General questions
Hi to all.
As I understand from "Collision detection" example, 2 bodies after creation collide without additional code, I mean box2d do it for me?
In my game one circleShape object must push another, but it passes through instead.
This is my main.lua. (My code a little bit rough)

local function is32bit()
return string.dump(is32bit):byte(9) == 4
end

if is32bit() then
require("tntvpad32")
else
require("tntvpad64")
end

require "box2d"

b2.setScale(10)

--background
local background = Bitmap.new(Texture.new("Images/background.JPG"))
background:setAnchorPoint(0.5, 0.5)
stage:addChild(background)

world = b2.World.new(0, 0)

--start create player
local startX = 10--application:getDeviceHeight()/2
local startY = 10--application:getDeviceWidth()/2

player_spr = Bitmap.new(Texture.new("Images/1-up.png"))
player_spr:setAnchorPoint(0.5, 0.5)
player_spr:setPosition(startX, startY)
local radius = player_spr:getWidth()/2

player_body = world:createBody{type = b2.KINEMATIC_BODY, position = {x = startX, y = startY}}
player_body.name = "player"
local player_shp = b2.CircleShape.new(startX, startY, radius)
player_body:createFixture{shape = player_shp, density = 1, restitution = 0.2, friction = 0.3}

stage:addChild(player_spr)

--create enemy
enemy_spr = Bitmap.new(Texture.new("Images/2-up.png"))
enemy_spr:setAnchorPoint(0.5, 0.5)
enemy_spr:setPosition(200, 200)

enemy_body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = 200, y = 200}}
enemy_body.name = "enemy"
local enemy_shp = b2.CircleShape.new(200, 200, radius)
enemy_body:createFixture{shape = enemy_shp, density = 1, restitution = 0.2, friction = 0.3}

stage:addChild(enemy_spr)

--VPAD
local vPad = CTNTVirtualPad.new(stage, "Images/tntskinpad", PAD.STICK_SINGLE,PAD.BUTTONS_NONE, 20, 2)
vPad:setJoyStyle(PAD.COMPO_LEFTPAD, PAD.STYLE_CLASSIC)
vPad:setJoyAsAnalog(PAD.COMPO_LEFTPAD, false)
vPad:setHideMode(PAD.MODE_NOHIDE)
vPad:start()

vPad:addEventListener(PAD.LEFTPAD_EVENT, move, self)

function onEnterFrame()
world:step(1/60, 8, 3)
enemy_spr:setPosition(enemy_body:getPosition())
end

stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

One more interesting fact: if I change in "Collision detection" example 31 line from PolygonShape.new() to CircleShape.new(x, y, 40) and comment 32 line, then one circle began to fall down into infinity=)
Thanks in advance.

Comments

  • zvardinzvardin Member
    Accepted Answer
    I suggest adding this to see what's going on:
    local dd = b2.DebugDraw.new()
    world:setDebugDraw(dd)
    stage:addChild(dd)
    For instance, in the Collision Detection example if you do what you said you'll see that then the circle isn't positioned correctly to match the sprites so the first body is off screen and won't hit the floor.
  • plamenplamen Member
    Accepted Answer
    local player_shp = b2.CircleShape.new(startX, startY, radius) - should be: local player_shp = b2.CircleShape.new(0, 0, radius) if you want the body fixture in the middle of the body thus to the image. Coordinates of the fixtures are local to the body center.
  • VeliasVelias Member
    edited January 2013
    Thx a lot, that my mistake. I thought that all coordinates are global.
    And thx for tip with DebugDraw.

    Likes: zvardin

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.