Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
About restitution — Gideros Forum

About restitution

Diego_85Diego_85 Member
edited May 2013 in General questions
Hello everybody, i'm trying to make a simple 15-puzzle game but i can't figure out why the tiles bounce when i move them fast one into another. I set all the bodies restitution to 0 and tried different values for density, friction and linear damping without success.

Here is my code:
 
require 'box2d'
 
local puzzle = Core.class(Sprite)
 
function puzzle:init()
 
	self.world = b2.World.new(0,0);
 
 
	local screenW = 402 
	local screenH = 402 
 
 
	self:wall(200,screenH/2+300,0, screenH)
	self:wall(screenW/2+200,300,screenW,0)
	self:wall(screenW+200,screenH/2+300,0,screenH)
	self:wall(screenW/2+200,screenH+300,screenW,0)
 
 
	self.tiles = {}
 
 
	local shift = 100
 
	for i = 1,15 do
 
		local xpos = ((i-1) % 4) * shift + 200
		local ypos =  math.floor( (i-1) / 4 ) * shift + 300
 
		self.tiles[i] = self:createBox(xpos,ypos,i)
	end
 
 
 
 
 
	local ground = self.world:createBody({})
 
	local mouseJoint = nil
 
 
	function self:onMouseDown(event)
 
		for index, tile in ipairs(self.tiles) do
 
			if tile:hitTestPoint(event.x, event.y) then
				local jointDef = b2.createMouseJointDef(ground, tile.body, event.x, event.y, 100000)
				mouseJoint = self.world:createJoint(jointDef)
				return
			end
 
		end
 
	end
 
 
	function self:onMouseMove(event)
		if mouseJoint ~= nil then
			mouseJoint:setTarget(event.x, event.y)
		end
	end
 
 
	function self:onMouseUp(event)
		if mouseJoint ~= nil then
			self.world:destroyJoint(mouseJoint)
			mouseJoint = nil
		end
	end
 
	function self:onPreSolve(e)
		local contact = e.contact
		contact:setRestitution(0)
	end
 
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
	self:addEventListener(Event.PRE_SOLVE, self.onPreSolve, self)
 
 
 
	--local debugDraw = b2.DebugDraw.new(false)
	--self.world:setDebugDraw(debugDraw)
	--self:addChild(debugDraw)
 
 
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
 
 
end
 
 
 
function puzzle:wall(x, y, width, height)
 
local wall = Shape.new()
 
	wall:setFillStyle(Shape.SOLID, 0xff0000) 
	wall:beginPath()
 
 
	wall:moveTo(-width/2,-height/2)
	wall:lineTo(width/2, -height/2)
	wall:lineTo(width/2, height/2)
	wall:lineTo(-width/2, height/2)
	wall:closePath()
	wall:endPath()
	wall:setPosition(x,y)
 
 
	local body = self.world:createBody{type = b2.STATIC_BODY}
	body:setPosition(wall:getX(), wall:getY())
 
	local poly = b2.PolygonShape.new()
	poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2)
	local fixture = body:createFixture{shape = poly, density = 0, 
	friction = 0, restitution = 0}
 
	wall.body = body
	wall.body.type = "wall"
 
 
	local body = self.world:createBody{type = b2.STATIC_BODY}
	body:setPosition(wall:getX(), wall:getY())
 
	local poly = b2.PolygonShape.new()
	poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2)
 
	wall.body = body
	wall.body.type = "wall"
 
 
	self:addChild(wall)
 
	return wall
 
end
 
function puzzle:createBox(x, y, num)
 
	local tile = Bitmap.new(Texture.new('./' .. num .. '.jpg'));
	-- 50 = half the tile dim	
	tile:setPosition(x+50,y+50)
	tile:setAnchorPoint(0.5, 0.5)
 
	local shape = b2.PolygonShape.new()
	shape:setAsBox(50, 50)
 
 
 
	local myBody = self.world:createBody{type = b2.DYNAMIC_BODY}
	myBody:setPosition(tile:getX(), tile:getY())
	myBody:setLinearDamping(5)
 
	myBody:createFixture{shape = shape, density = 0, restitution = 0, friction = 0}
 
 
	tile.body = myBody
	tile.body.type = "tile"
	self:addChild(tile)
 
	return tile
 
end
 
function puzzle:onEnterFrame()
	self.world:step(1/60, 8, 3)
	for i = 1, self:getNumChildren() do
 
		local sprite = self:getChildAt(i)
 
		if sprite.body then
 
			local body = sprite.body
 
			local bodyX, bodyY = body:getPosition()
 
			sprite:setPosition(bodyX, bodyY)
 
		end
	end
end
 
 
local mainScene = puzzle.new()
 
stage:addChild(mainScene)

Comments

  • From my experience, setting restitution = 0 does not guarantee that there will be no bouncing. You can try combine these code:
    b2.setScale(100)
     
    --ground setting
    local ground = world:createBody({})
    	local groundShape = b2.EdgeShape.new(groundX1, groundY1, groundX2, groundY2)
    	ground:createFixture({shape = groundShape, density = 0, restitution = 0.0, friction = 0.0})
     
    --items setting
    local bombBody = world:createBody{type = b2.DYNAMIC_BODY, position = {x = bombX, y = bombY}}
    	local bombShape = b2.PolygonShape.new()
    	bombShape:setAsBox(24, 24)
    	bombBody:createFixture{shape = bombShape, density = 0, restitution = 0.0, friction = 0.0}
     
    --on enter frame set
    world:step(1/60, 8, 3)
    world:step(1/60, 8, 3)
    world:step(1/60, 8, 3)
    It can reduce the bounce (just reduce). My solution is manual control items and their movement: https://play.google.com/store/apps/details?id=com.ultimategamestudio.monstermine
  • I'm sorry what should do the trick? The different scale? The rest seems identical to what i did. Tried the different scale without success anyway..
  • ar2rsawseenar2rsawseen Maintainer
    you can retrieve contact info on collision and reset restitution of whole collision like this:
    function onCollision(e)
        local contact = e.contact
        contact:setRestitution(0)
    end
  • Diego_85Diego_85 Member
    edited May 2013
    I think I did it in the code at the init() function. But i'm very new here maybe i did it wrong :D Is that correct?
  • OZAppsOZApps Guru
    edited May 2013
    Have you tried to change the friction to 1, totally frictionless (friction=0) means it would glide, right? I guess that is what causes the bounce.

    Though I would make all the boxes static and then make the one touched dynamic, more importantly, why would you want to use physics for moving tiles
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Have you tried to change the friction to 1, totally frictionless (friction=0) means it would glide, right? I guess that is what causes the bounce.
    Yes I did, still doesn't work, is this a bug or I'm misunderstanding something? From what I know restitution should be something like an elastic coefficient to regulate bounces ecc..
  • try my sample, a bit mess but it worked ;)
    rar
    rar
    Fruit Pop.rar
    1M
  • Hello @Diego_85 see us game New Slide Puzzle...totaly make with fantastic gideros....
Sign In or Register to comment.