Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to implement sprite climbing hill in box2d? — Gideros Forum

How to implement sprite climbing hill in box2d?

louislouis Member
edited March 2013 in General questions
I want to write a game, this game is a runner style, character climb up and down in hill.
My question,if I use box2d, I referred the giderios sample projfect "collision filtering",the ball always is up and down in ground ,how to implement to ball auto climb top height of hill, continue to move forward, not move back?

This is my favorite game:
https://play.google.com/store/apps/details?id=com.fingersoft.hillclimb

video:

Comments

  • I think this will be the first game to be installed on my Nexus 4 when it arrives today :D
  • ar2rsawseenar2rsawseen Maintainer
    well with Hill Climb Racing it's easy, you simply apply motor to the wheels and based on friction they drive up the hill.
    But what do you mean by auto climb?
  • louislouis Member
    Hill Climb Racing ,user control speed up, but in my game ,I want to character always running, don't need user speed up or brake, when character ran to a slope, according to angle,automatic speed down ,finally the character will be able to climb up ,and then run down from the top point, according to angle will be running faster and faster; character has infinite climb hill, down hill....

  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013
    Oh I get it now, here is something similar discussed here:
    http://www.giderosmobile.com/forum/discussion/2331/can039t-stop-character-sliding-down-slope#Item_1

    Edit:
    scratch that, thats completely opposite (still can view for some ideas) :D

    In your case, I think what you want to achieve is going app the slope with same speed?

    That's an interesting one. You can try two approaches:
    Apply constant force and remove friction
    Or as you've mentioned detecting when you are on slope and applying more/less force.

    So what exactly gives you the problems? Detection of slope, calculation of force? Or just looking if there is any easier solution? :)

  • hgvyas123hgvyas123 Guru
    edited March 2013
    @louis :-

    are u trying to make some what like tiny wings?

    Likes: fxone

    +1 -1 (+1 / -0 )Share on Facebook
  • louislouis Member
    edited March 2013
    Thanks,@ar2rsawseen ,@hgvyas123 ,

    Actually, I'm newbie of box2d, so my question is very elementary, or very stupid,now I want to slove problems are:

    1.how to detect slope,up,or down?
    2. how to calculate body impulse? now I use body:applyLinearImpulse() method, has any better solution?
    3. how to calculate and set character angle,when climb hill , body rotation 45 degree,down hill,body rotation -45 degree
    4. how to remove bounce(jump more than once) of body when body land from air?

    This is my test code:
    --[[
     
    This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
    (C) 2010 - 2011 Gideros Mobile 
     
    ]]
     
    require "box2d"
     
    --b2.setScale(30)
     
    -- this table holds the dynamic bodies and their sprites
    local actors = {}
     
    -- create world
    local world = b2.World.new(0, 4)
     
    -- create ground bitmap and add to stage
    local groundBitmap = Bitmap.new(Texture.new("ground.png"))
    groundBitmap:setAnchorPoint(0, 1)
    groundBitmap:setY(320)
    --stage:addChild(groundBitmap)
     
     
     
    local function ground()
    	-- create ground body
    	local ground = world:createBody({})
     
    	ground:setPosition(0,0)
     
    	-- ground body is consists of 24 tiny edges
    	local shape = b2.EdgeShape.new()
    	local groundFilter = {categoryBits = 1, maskBits = 6}
     
    	local fixtures={}
    	for x=0,480,20 do
    		print(x)
    		local x1 = x
    		local x2 = x +20
    		local y1 = math.cos((x1 - 240) * 0.01) * 30 + 240
    		local y2 = math.cos((x2 - 240) * 0.01) * 30 + 240
    		--local y1=((x1 - 240) * 0.02)+240
    		--local y2=((x2 - 240) * 0.02)+240
    		print(y1,y2)
     
    		shape:set(x1, y1, x2, y2)
    		local f=ground:createFixture({shape = shape, density = 1, filter})
    		fixtures[#fixtures+1]=f
    	end
    	return ground
    end
     
    local g1=ground()
    local g2=ground()
     
     
     
     
     
    -- create a circle body with given texture and collision filter and add it to stage
    local function createCircle(texture, x, y, filter)
    	local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = x, y = y}}
     
    	local shape = b2.CircleShape.new(0, 0, 25)
    	body:createFixture{shape = shape, density = 1, restitution = 0, friction = 0, filter = filter}
     
    	--body:setLinearDamping(10)
     
    	--body:applyTorque(100)
    --body:setAngularDamping(-10)
     
    --[[
    	local sprite = Bitmap.new(Texture.new(texture, true))
    	sprite:setAnchorPoint(0.5, 0.5)
    	stage:addChild(sprite)
    	actors[body] = sprite
    	]]--
     
    	actors[body] = true
     
    end
     
    -- create circles
    local redFilter = {categoryBits = 2, maskBits = 3}
    local blueFilter = {categoryBits = 4, maskBits = 5}
     
    createCircle("red-circle.png", 120, 240, redFilter)
    --createCircle("red-circle.png", 120, 100, redFilter)
    --createCircle("red-circle.png", 180, 100, redFilter)
     
    createCircle("blue-circle.png", 480-60, 200, blueFilter)
    --createCircle("blue-circle.png", 480-120, 100, blueFilter)
    --createCircle("blue-circle.png", 480-180, 100, blueFilter)
     
     
    local bgFarY
    local newFarY
    -- step the world and then update the position and rotation of sprites
    local function onEnterFrame()
    	world:step(1/60, 8, 1)
     
    	for body,sprite in pairs(actors) do
     
    		body:applyLinearImpulse(0.025,0.025,body:getWorldCenter())
     
    		print(body:getPosition())
    		--[[
    		sprite:setPosition(body:getPosition())
    		sprite:setRotation(body:getAngle() * 180 / math.pi)
    		]]--
     
     
    		bgFarY = (bgFarY or 0) - ((8 or 4.0) / 4)
    		print(bgFarY)
    		--self.bgNearY = (self.bgNearY or 0) - ((self.bgspeed or 4.0))
     
    		newFarY = 480 -(-bgFarY)
    		if newFarY <=0 then
    			bgFarY = 0
    			g1:setPosition(bgFarY,0)
    		else
    			g1:setPosition(bgFarY,0)
    			g2:setPosition(newFarY,0)
    		end
     
    	end
    end
     
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
     
     
    local debugDraw = b2.DebugDraw.new()
    world:setDebugDraw(debugDraw)
    stage:addChild(debugDraw)
  • louislouis Member
    @hgvyas123 ,yes ,like tiny wings,any idea?
  • hgvyas123hgvyas123 Guru
    Accepted Answer
    i dont know about your game play heres the how tiny wing works

    on the touch it is increasing gravity and on release it is setting gravity to normal you also need to continuos chk whether the force on body is not slow then any desired amount if yes apply some linearvelocity or force in x direction only and let box2d to convert it accordingly.

    for the rotation i think this code can help
     
    local sprite = Bitmap.new(Texture.new("a.png"))
    sprite:setAnchorPoint(0.5,0.5)
    sprite.lastX = sprite:getX()
    sprite.lastY = sprite:getY()
    sprite.angle = sprite:getRotation()
     
    local angle = 0
    local prevAngle = sprite:getRotation()
    local function onEnterFrame()
     
    	angle = math.atan2(sprite:getY() - sprite.lastY,sprite:getX() - sprite.lastX);
    	angle = angle * (180/3.1428)
     
    	sprite.lastX = sprite:getX()
    	sprite.lastY = sprite:getY()
     
    	sprite:setRotation(sprite:getRotation() + angle-prevAngle);
     
    	prevAngle = angle;
     
    end
    stage:addEventListener(Event.ENTER_FRAME,onEnterFrame,stage)
    to detect the slope you can chk the angle of our sprite

    and to remove the bounce i think setting restitution parameter to 0 for both hill and sprite will work

    :)

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • I think this will be the first game to be installed on my Nexus 4 when it arrives today :D
    So you finally got yourself a new toy then?
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • Yeah but the wife took the cat to the vet and got back 20 mins after they tried to deliver it yesterday :(

    She is under orders not to leave the house today :)

    Likes: techdojo

    +1 -1 (+1 / -0 )Share on Facebook
  • =))
    That would be so funny if wasn't too true far too often, especially with wives that seem to think their stuff is waaay more important.

    Couldn't they deliver it to the office?
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • It's been delivered so now I'm waiting for 5pm. Some companies will only deliver to the address the card is registered to.
  • MellsMells Guru
    edited March 2013
    @louis

    3. how to calculate and set character angle,when climb hill , body rotation 45 degree,down hill,body rotation -45 degree
    Similar to @hgvyas123 with code sample provided by @atilim so you can understand better what's happening :

    -> Align Sprite Axis to Vector

    sorry to be on-topic :)
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • hgvyas123hgvyas123 Guru
    edited March 2013
    first forum where person have to say sorry for being on topic

    :))
Sign In or Register to comment.