Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to create trajectory (Same as in angry bird) for archery game? — Gideros Forum

How to create trajectory (Same as in angry bird) for archery game?

SarthakSarthak Member
edited August 2014 in General questions
Hi,

I have created basic archery game, but now i want to display trajectory based on drag distance (which same as angry bird), so please help me or suggest me how can i get it ?
Give me some guidance , how do i achieve this?

I have seen below post ,
http://giderosmobile.com/forum/discussion/2130/drawing-a-direction-path/p1
but i am not able to get it. So please help me.

Thank you.

Likes: Sarthak

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • hgvyas123hgvyas123 Guru
    Accepted Answer
    Try this 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, 9.8)
     
    local DummyWorld = b2.World.new(0, 9.8)
     
    -- 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)
     
    -- create ground body
    local ground = world:createBody({})
     
    -- ground body is consists of 24 tiny edges
    local shape = b2.EdgeShape.new()
    local groundFilter = {categoryBits = 1, maskBits = 6}
    for x=0,460,20 do
    	local x1 = x
    	local x2 = x + 20
    	local y1 = math.cos((x1 - 240) * 0.01) * 80 + 240
    	local y2 = math.cos((x2 - 240) * 0.01) * 80 + 240
     
    	shape:set(x1, y1, x2, y2)
    	ground:createFixture({shape = shape, density = 0, filter})
    end
     
    -- create a circle body with given texture and collision filter and add it to stage
     
    local function onTouchDown(self,event)
     
    		self.isFocus = true
    		self.startX = event.x
    		self.startY = event.y
     
    end
     
    local xPosArr = {}
    local yPosArr = {}
    local myCircles = {}
     
    function drawArc(xc,yc,xradius,yradius,startAngle,endAngle,isFill)
    	if yradius == nil then
    		yradius = xradius
    	end
    	if startAngle == nil then
    		startAngle = 0
    	end
    	if endAngle == nil then
    		endAngle = 360
    	end
    	if isFill == nil then
    		isFill = true
    	end
    	local shape = Shape.new()
    	if isFill then
    		shape:setFillStyle(Shape.SOLID, 0x000000, 0.5)
    	else
    		shape:setLineStyle(3, 0x000000)
    	end
    	shape:beginPath()
    	for i=startAngle,endAngle do
    		if i==1 then
    			shape:moveTo(math.sin(math.rad(i)) * xradius, math.cos(math.rad(i)) * yradius)
    		else
    			shape:lineTo(math.sin(math.rad(i)) * xradius, math.cos(math.rad(i)) * yradius)
    		end
    	end
    	if isFill then
    		shape:closePath()
    	end
    	shape:endPath()
    	shape:setPosition(xc,yc)
    	return shape
    end
     
    for i=1,40 do
    	myCircles[i] = drawArc(-100,100,2,2)
    	stage:addChild(myCircles[i])
    end
    local function onTouchMove(self,event)
    	if self.isFocus then
    		print("called")
    		local body = DummyWorld:createBody{type = b2.DYNAMIC_BODY, position = {x = stage.sprite:getX(), y = stage.sprite:getY()}}
     
    		local shape = b2.CircleShape.new(0, 0, 12)
    		body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 0.1}
     
    		local xVect = (self.startX-event.x)*0.07
            local yVect = (self.startY-event.y)*0.07
            --applye impulse to target
            body:applyLinearImpulse(xVect, yVect, body:getPosition())
     
    		for i=1,40 do
    			DummyWorld:step(1/60, 8, 3)
    			local x,y = body:getPosition()
     
     
    			myCircles[i]:setPosition(x,y)
    		end
     
    		DummyWorld:destroyBody(body)
    	end
    end
     
    local function onTouchUp(self,event)
    	if self.isFocus then
    		self.isFocus = false
    		for i=1,40 do
    			myCircles[i]:setPosition(-100,100)
    		end
    		local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = stage.sprite:getX(), y = stage.sprite:getY()}}
     
    		local shape = b2.CircleShape.new(0, 0, 12)
    		body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 0.1}
    		stage.sprite.body = body
    		actors[body] = stage.sprite
    		 local xVect = (self.startX-event.x)*0.07
            local yVect = (self.startY-event.y)*0.07
            --applye impulse to target
            body:applyLinearImpulse(xVect, yVect, body:getPosition())
    	end
    end
     
    local function createCircle(texture, x, y, filter)
    	--local body = world:createBody{type = b2.KINEMATIK_BODY, position = {x = x, y = y}}
     
    	--local shape = b2.CircleShape.new(0, 0, 12)
    	--body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 0.1}
     
    	local sprite = Bitmap.new(Texture.new(texture, true))
    	sprite:setPosition(240,210)
    	sprite:setScale(0.5,0.5)
    	sprite:setAnchorPoint(0.5, 0.5)
    	stage:addChild(sprite)
     
     
    	stage.sprite = sprite
    	--actors[body] = sprite
    end
     
    -- create circles
     
     
    createCircle("blue-circle.png", 240, 160, blueFilter)
     
    local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = 100, y = 90}}
     
    local shape = b2.CircleShape.new(0, 0, 12)
    body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 0.1}
     
    local sprite = Bitmap.new(Texture.new("blue-circle.png", true))
    sprite:setPosition(240,210)
    sprite:setScale(0.5,0.5)
    sprite:setAnchorPoint(0.5, 0.5)
    stage:addChild(sprite)
     
    actors[body] = sprite
     
    -- step the world and then update the position and rotation of sprites
    local function onEnterFrame()
    	world:step(1/60, 8, 3)
     
    	for body,sprite in pairs(actors) do
    		sprite:setPosition(body:getPosition())
    		sprite:setRotation(body:getAngle() * 180 / math.pi)
    	end
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
     
    stage:addEventListener(Event.MOUSE_DOWN,onTouchDown,stage)
    	stage:addEventListener(Event.MOUSE_MOVE,onTouchMove,stage)
    	stage:addEventListener(Event.MOUSE_UP,onTouchUp,stage)
     
    local debugDraw = b2.DebugDraw.new()
    world:setDebugDraw(debugDraw)
    stage:addChild(debugDraw)
    +1 -1 (+3 / -0 )Share on Facebook
  • :)
    Thank you.

    Likes: Sarthak

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