Help getting my bullet to shoot in the proper direction

Hello I am creating a game where a dart gun is rotated constantly at the bottom of the screen. When the player touches the screen I want to shoot a dart out of the gun. I want it to shoot the direction the gun is facing. I have been able to get the dart to spawn in the proper direction but I can’t figure how to make the dart shoot across the screen. Any help would be much appreciated.

[code]
–function for firing the dart
local function fireDart(event)
local dart = display.newImage(“images/Lvl_1/dart.png”)
dart:setReferencePoint(display.CenterReferencePoint);
dart.x = gun.x
dart.y = gun.y
dart.rotation = gun.rotation
local speed = 5

local velocity = {}
velocity.x = math.cos( math.rad( dart.rotation - 90 ) )
velocity.y = math.sin( math.rad( dart.rotation - 90 ) )

[code] [import]uid: 49863 topic_id: 15330 reply_id: 315330[/import]

Looks like you’re pretty much there

velocity.x = math.cos(math.rad(dart.rotation-90)) \* speed velocity.y = math.sin(math.rad(dart.rotation-90)) \* speed dart:applyForce(velocity.x, velocity.y, gun.x, gun.y) [import]uid: 21331 topic_id: 15330 reply_id: 56661[/import]

That didn’t work the dart spawns and doesn’t move. In the console it says… runtime error attempt call method ‘applyForce’ a nil value [import]uid: 49863 topic_id: 15330 reply_id: 56677[/import]

sorry, took for granted that you had applied physics. you must add it to the physics simulation
Please do some reading on this, but:

physics.addBody(dart, “dynamic”, {density=1.0, friction=0.5, bounce=0.1})

Do some reading on this, you will probably want to set params to your liking. Don’t forget to turn on the physics as well, you will see when you read up on it.
[import]uid: 21331 topic_id: 15330 reply_id: 56688[/import]

Yeah I had that in there at some point. Sometimes it becomes easy to forget to bring something back in after you have taken it out. With this added I am still not getting anywhere. It spawns the dart and the dart just sits there. Except for now when I shoot a second dart it moves the first dart up a little bit. I am going to post my code for the whole level maybe there is something else that I am just missing. Sorry for the huge post but at least this way you can see all of the code I have for this level.

[code]
module(…, package.seeall)

–====================================================================–
– SCENE: BALLOON LEVEL
–====================================================================–

–[[

  • Version: 0.1
  • Made by Ninja Carnival Team @ 2011

******************

  • INFORMATION
    ******************

  • Balloon Level

–]]

new = function ( params )

– Imports
local ui = require ( “ui” )

– Groups
local localGroup = display.newGroup()

– Variables
local levelTimer = 15

– start physics
local physics = require(“physics”)
physics.start()
physics.setGravity(0,0 )

– Display Objects
local background = display.newImage(“images/Lvl_1/Level_1_BG.png”)

local gun = display.newImage(“images/Lvl_1/goldenGun3.png”)

– Player Stats
local statTextSize = 16
local score = 0
local scoreNum = display.newText(0, 0, 0, native.systemFontBold, statTextSize)
local scoreText = display.newText(“SCORE:”, 0, 0, native.systemFontBold, statTextSize)
local gameTimer = 60
local gameTimerText = display.newText(“60”, 0, 0, native.systemFontBold, statTextSize)
local statsBackground = display.newRect(0,0,_W,scoreText.height+2.5)

– BUTTONS

– FUNCTIONS

–rotate gun
local rotDirection = 1
local function gunRotation(event)
gun.rotation = gun.rotation + rotDirection
if gun.rotation > 90 then
rotDirection = -1
elseif gun.rotation < -80 then
rotDirection = 1
end
end

–function for firing the dart
local function fireDart(event)
local dart = display.newImage(“images/Lvl_1/dart.png”)
dart:setReferencePoint(display.CenterReferencePoint);
dart.x = gun.x
dart.y = gun.y
dart.rotation = gun.rotation
physics.addBody(dart, “dynamic”, {density=1.0, friction=0.5, bounce=0.1})
local speed = 5
local velocity = {}
velocity.x = math.cos( math.rad( dart.rotation - 90 ) )
velocity.y = math.sin( math.rad( dart.rotation - 90 ) )
dart:applyForce(velocity.x, velocity.y, dart.x, dart.y)

end

– PARAMETERS

– INITIALIZE
local initVars = function ()
– Inserts
localGroup:insert(background)
localGroup:insert(statsBackground)
localGroup:insert(scoreText)
localGroup:insert(gameTimerText)

– Positions

– score positions
scoreText.x = (scoreText.width * 0.5) + 10
scoreText.y = (scoreText.height * 0.5) + 5
scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
scoreNum.x = scoreText.width + 5
scoreNum.y = scoreText.y

– timer position
gameTimerText.x = _W * 0.5
gameTimerText.y = scoreText.y

– gun position
gun:setReferencePoint(display.centerReferencePoint);
gun.x = display.contentWidth / 2
gun.y = display.contentHeight - 30

– Colors
–background:setFillColor(255,255,255)
statsBackground:setFillColor(50,0,0)

scoreText:setTextColor(255,0,0)
scoreNum:setTextColor(255,0,0)
gameTimerText:setTextColor(255,0,0)

– Listeners
–Runtime:addEventListener(“enterFrame”, update)
Runtime:addEventListener( “enterFrame”, gunRotation);
Runtime:addEventListener(“tap”, fireDart);

end

– Initiate variables
initVars()

– Update
local update = function ( event )

end

local updateTimer = function( event )
gameTimer = gameTimer - 1
gameTimerText.text = gameTimer
scoreNum.text = (scoreNum.text+1) * 5
scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
scoreNum.x = scoreText.width + 5
scoreNum.y = scoreText.y

end
timer.performWithDelay(1000, updateTimer, 60)

– Clear objects
local clear = function ()

end

– MUST return a display.newGroup()
return localGroup
end

[code] [import]uid: 49863 topic_id: 15330 reply_id: 56691[/import]

I’ll test as soon as I can, but don’t forget the “speed” change in my first post.

You also did not add params like i gave in first post. your applyForce should use the gun.x and gun.y as shown, although it would work as you have your logic, it is bad habbit. [import]uid: 21331 topic_id: 15330 reply_id: 56692[/import]

No need it was the speed issue. I don’t know how I overlooked it but that’s why I posted my whole level. Thank you very much for your help. [import]uid: 49863 topic_id: 15330 reply_id: 56696[/import]

Sweet! Np. Good Luck! [import]uid: 21331 topic_id: 15330 reply_id: 56700[/import]