Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Scrolling and physics — Gideros Forum

Scrolling and physics

Tom2012Tom2012 Guru
edited October 2012 in General questions
I have a game that uses scrolling and physiscs.

Trouble is, the ground is scrolling but the physics bodies are not 'attached' to it. They ignore the scrolling.

Should I create the physics world on the scrolling layer, or is there a better way to do it?

Thanks

Comments

  • How did you know that the physics bodies are not 'attached'? Are you using DebugDraw?
  • @Tom2012, can you exemplify what's really going on? It would be helpful if you include code sample here so people can analyze....
  • @Tom, are you scrolling the ground by altering the x/y coordinates? and are you also scrolling the physics bodies in the same way or are you expecting them to update in the enter frame?
    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
  • Thanks guys

    I found the solution was to add the physics world to a separate layer to that of the scene:
    local physicsLayer = Sprite.new() -- create new sprite instance
    physicsLayer.world = b2.World.new(0, 10, true) -- add the physics on this layer
    self:addChild(physicsLayer); -- add sprite to this scene
    I can then scroll the layer with my scroll class:
    Scrolling = Core.class(Sprite);
     
    function Scrolling:init(ground,bg1,bg2,hector,physicsLayer)
     
    -- Variables
     
    self.groundLeftEdge = 0;
    self.groundRightEdge = 0;
     
    self.ground = ground;
    self.bg1 = bg1;
    self.bg2 = bg2;
    self.hector = hector;
    self.physicsLayer = physicsLayer;
     
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
     
    end
     
     
     
    -- Enterframe loop
     
    function Scrolling:onEnterFrame()
     
     
    	if(self.hector.moving == "left") then
     
    		if(self.hector:getX() < 160 + (self.hector:getWidth()/2)) then
    			self.groundLeftEdge = self.ground:getX() - (self.ground:getWidth()/2)
     
    			if(self.groundLeftEdge < 0) then
    				self.bg2:setX(self.bg2:getX() + self.hector.speed * .45)
    				self.bg1:setX(self.bg1:getX() + self.hector.speed * .3)
    				self.ground:setX(self.ground:getX() + self.hector.speed)
    				self.physicsLayer:setX(self.physicsLayer:getX() + self.hector.speed) -- move the phsics world too
    			else
    				if(self.hector:getX() > 37) then
    					self.hector:setX(self.hector:getX()-self.hector.speed) -- no scrolling, hector walk
    				else
    				self.hector.moving = "stopped";
    				end
    			end
    		else
    		self.hector:setX(self.hector:getX() - self.hector.speed)
    		end
     
    	end
     
    	if(self.hector.moving == "right") then
     
    		if(self.hector:getX() > (160 - (self.hector:getWidth() / 2))) then
    			self.groundRightEdge = self.ground:getX() + (self.ground:getWidth()/2)
     
    			if(self.groundRightEdge > application:getContentWidth()) then
    				self.bg2:setX(self.bg2:getX() - self.hector.speed * .45)
    				self.bg1:setX(self.bg1:getX() - self.hector.speed * .3)
    				self.ground:setX(self.ground:getX() - self.hector.speed)
    				self.physicsLayer:setX(self.physicsLayer:getX() - self.hector.speed) -- move the phsics world too
    			else
    				if(self.hector:getX() < 283) then
    					self.hector:setX(self.hector:getX() + self.hector.speed)
    				else
    				self.hector.moving = "stopped";
    				end
    			end
    		else
    		self.hector:setX(self.hector:getX() + self.hector.speed)
    		end
     
    	end
     
     
     
     
    end
Sign In or Register to comment.