Shoot bullet in direction of Joystick Angle

Hey Everyone,

I’m stuck at the moment and could really use some help.

I’m currently using the “lib_analog_stick.lua” class to spawn a joystick and am trying to shoot an a bullet in the direction of the joystick’s angle, but I cannot figure out how to get the bullet to go in the right direction. (top-down game)

I think my code will best explain the situation, any help would be awesome.

local speed = 1000  
  
local movementScale = {}  
movementScale.x = math.cos( math.rad( stickRight:getAngle() ) )  
movementScale.y = math.sin( math.rad( stickRight:getAngle() ) )  
  
local velocity = {}  
velocity.x = movementScale.x \* speed  
velocity.y = movementScale.y \* speed  
  
bullet:applyForce( velocity.x, velocity.y, player.x, player.y)  

The problem is that the bullet seems to go in almost random directions, and will never go to the left (decreasing X values). [import]uid: 62528 topic_id: 17684 reply_id: 317684[/import]

  
local bulletSpeed = 350  
  
bullet:setLinearVelocity(math.sin(math.rad(stickRight:getAngle())) \*bulletSpeed, math.cos(math.rad(stickRight:getAngle())) \* -bulletSpeed)  
  

[import]uid: 84637 topic_id: 17684 reply_id: 67316[/import]

Thanks for your reply, Danny!

For some reason, using this code, I still have the same problem. It seems that the values being sent to setLinearVelocity are correct, but any negative values are being treated as positives. (for example, tilting the joystick to the left sets the x velocity to -349, but it goes to the right) [import]uid: 62528 topic_id: 17684 reply_id: 67317[/import]

Really? That works for me,

Could you post up some more code? [import]uid: 84637 topic_id: 17684 reply_id: 67321[/import]

Sure, Thanks again.

[code]
local function main()

stickLeft:move(player, 2.15, true)

local rightStickMoving = math.ceil(stickRight:getPercent()*100)

local function shoot()
–print(“blam!”)

local bullet = display.newImage(“images/bullet.png”)
bullet:setReferencePoint(display.CenterReferencePoint)
bullet.x = player.x
bullet.y = player.y
bullet:scale(.25,.275)
physics.addBody( bullet, “dynamic”, { isSensor=false, density=1, friction=0.3, bounce=0.2, radius=h*.021} )
bullet.rotation = player.rotation
bullet.name = “bullet”
bulletGroup:insert(bullet)

local bulletSpeed = 350

bullet:setLinearVelocity(math.sin(math.rad(stickRight:getAngle())) *bulletSpeed, math.cos(math.rad(stickRight:getAngle())) * -bulletSpeed)
bullet:applyAngularImpulse(50)

end

if(rightStickMoving ~= 0)then
if not shootTimer then
shootTimer = timer.performWithDelay(500, shoot, 0)
end
else
if(shootTimer)then
timer.cancel(shootTimer)
shootTimer = nil
end
end

– SHOW STICK INFO
–print(“ANGLE = “…stickLeft:getAngle()…” DISTANCE = “…math.ceil(stickLeft:getDistance())…” PERCENT = “…math.ceil(stickLeft:getPercent()*100)…”%”)

end
[/code] [import]uid: 62528 topic_id: 17684 reply_id: 67322[/import]

Ah, I’ve figured it out! The bullet was reacting with the player’s physics body! A collision filter should solve my problem.

I have been stuck on this for a few days and wouldn’t have come to this conclusion without your help / verification that your code worked.

Thanks a ton!

  • Clay [import]uid: 62528 topic_id: 17684 reply_id: 67328[/import]