Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
fire arrow at given angle — Gideros Forum

fire arrow at given angle

hgvyas123hgvyas123 Guru
edited September 2013 in General questions
what should i need to do if i have to fire arrow at any angle say 45,60,30 ? i know the center pos of arrow and its angle now how to fire in that direction

Comments

  • this is the what i have done but it is not working any idea or other solution
    local myRot = self.mySphere:getRotation()
    			self.mySphere:setRotation(0)----to easily create the physics body
    			local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = self.mySphere:getX(), y = self.mySphere:getY()}}
     
    			body.name = name
     
    			local shape = b2.PolygonShape.new()
    			shape:setAsBox(self.mySphere:getWidth()*0.5, self.mySphere:getHeight()*0.5)
    			body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 0.3}
    			self.mySphere.body = body
     
    			self.mySphere.body:setAngle(myRot*math.pi/180)--to reset the angle of sphere
     
    			--find 4 points to throw the sphere
    			local point1X = self.mySphere:getX()+self.mySphere:getWidth()*math.cos(myRot*math.pi/180)	
    			local point1Y = self.mySphere:getY()+self.mySphere:getHeight()*math.sin(myRot*math.pi/180)
    			local point2X = self.mySphere:getX()-self.mySphere:getWidth()*math.cos(myRot*math.pi/180)	
    			local point2Y = self.mySphere:getY()-self.mySphere:getHeight()*math.sin(myRot*math.pi/180)
     
    			local strength = 0.1
     
    			local xVect = (point1X-point2X)*strength
    			local yVect = (point1Y-point2Y)*strength
     
     
    			self.mySphere.body:applyLinearImpulse(xVect, yVect, self.mySphere:getX(),self.mySphere:getY())
  • ar2rsawseenar2rsawseen Maintainer
    edited September 2013 Accepted Answer
    If you have angle, you only need to calculate the vectors based on the angle:
    local xVect = -math.sin(self.mySphere.body:getAngle())*strength
    local yVect = math.cos(self.mySphere.body:getAngle())*strength
    self.mySphere.body:applyLinearImpulse(xVect, yVect, self.mySphere:getX(),self.mySphere:getY())
    depending on your original image, how it is rotated, you may need to apply offset angle, like your arrows image is pointing to the right, rotate it for 90 degrees by applying -math.pi/2 to make the default direction up
    local xVect = -math.sin(self.mySphere.body:getAngle()-math.pi/2)*strength
    local yVect = math.cos(self.mySphere.body:getAngle()-math.pi/2)*strength
    self.mySphere.body:applyLinearImpulse(xVect, yVect, self.mySphere:getX(),self.mySphere:getY())
  • yeah it worked i was fighting with some points since almost two hours

    :)
Sign In or Register to comment.