I currently have a tank with a turret, attached via physics joint, and I am trying to figure out how to fire a bullet in the direction my turret is pointing…
My goal is to fire a bullet at the beginning of the touch event ( when the user ‘taps’ the screen) and at the ‘end’ of the touch event.
*Very similar to the TinyTanks game.
Q1. How do I fire a bullet from the tip of my turret’s ‘cannon’ in whatever direction the turret is currently facing?
Q2. How do I code the bullet to ricochet off walls, preferably only once, canceling on the second collision…?
My turret is controlled by the user either touching &or dragging their finger within the display, and my thought was that I could using the ‘angle between’ portion of the code (kudos to Brent Sorrentino ) to calculate the direction the bullet should fire, but I have been experimenting all day trying to find the answer and have had little success.
This is the code I am using to ‘aim’ my turret: [lua]
–adds a rect to act as a base for touch events relating to the turret
local transRect = display.newRect(0,0, 1024, 768)
transRect.isVisible = false
transRect.isHitTestable = true
local math_deg = math.deg --localize ‘math.deg’ for better performance
local math_atan2 = math.atan2 --localize ‘math.atan2’ for better performance
–likewise, this function should reside outside and above the touch function
local function angleBetween( user_turretX, user_turretY, eventX, eventY )
local angle = ( math_deg( math_atan2( eventY-user_turretY, eventX-user_turretX ) )+90 ) --; return angle
if ( angle < 0 ) then angle = angle + 360 end ; return angle % 360
end
local function touchRect( event )
local ang = angleBetween( user_turret.x, user_turret.y, event.x, event.y )
user_turret.rotation = ang --rotate turret on ALL touch/drag conditions
local e = event.phase
if e.phase == “began” or e.phase == “moved” then
print( “BEGAN/MOVED phase touch” )
elseif e.phase == “ended” then
print( “FIRE a bullet now!” )
end
end
transRect:addEventListener( “touch”, touchRect )[/lua]
P.S. Is there a way for me to delete my old forum posts?
Thanks in advance for any replies. 
-Saer [import]uid: 148623 topic_id: 32510 reply_id: 332510[/import]
