How to Rotate a Turret by dragging?

Hi, I have been learning Corona for the past 4 weeks (off and on), so yes, I am quite new.

Question:

(1) How do you code to rotate a turret around a tank (from 0 to 90 degrees) by simply dragging it with your finger?

(2) Also, how do you code to fire a ball from the end of the turret, no matter the angle the turret is at? (something like creating an invisible ball to follow the end of the turret? Could be wrong)

So far:
(1) I have a tank
(2) I can shoot a ball from the end of the turret
(3) I can hit objects with the ball fired and cause objects to disappear.

Any help would be much appreciated!! Thank you :slight_smile:
[import]uid: 46629 topic_id: 9122 reply_id: 309122[/import]

The functions you require are in my Rum library - http://monkeydeadstudios.com/rum.php?page=math - they are all common functions so feel free to download Rum and then just copy out the code you want if you don’t want to include the full library.

For the first question you would do something like this:

  
require("rum")  
  
local onTouch = function( event )  
  
 local angle = math.angleBetween( tank, event )   
  
 turret.rotation = angle  
  
end  
  
Runtime:addEventListener( "touch", onTouch )  
  

Then to move your bullet in the right direction you would do something like this:

[code]

local bullet = display.newImage( “bullet.png” )

local speed = 5

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

local onUpdate = function( event )
bullet:move( velocity.x * speed, velocity.y * speed )
end

Runtime:addEventListener( “enterFrame”, onUpdate )

[/code] [import]uid: 5833 topic_id: 9122 reply_id: 33258[/import]

What is the use of “rum”? To copy/paste lines of codes? [import]uid: 54241 topic_id: 9122 reply_id: 33266[/import]

Rum is simply the combination of a bunch of functions I have written ( or common math functions as used for this thread ) that make things easier for me when working on projects. [import]uid: 5833 topic_id: 9122 reply_id: 33268[/import]

Thank you GrahamRanson for your reply!

However, I do not know quite how to add your library to my main.lua file. I have made the adjustments I thought necessary but it doesnt seem to work. I downloaded your Rum Library, with the demo file as well. I added your rum.lua file to my app folder that contains the rest of me files for the app and pasted in the code you described above but it seems to not work. Any ideas why? Thanks! [import]uid: 46629 topic_id: 9122 reply_id: 33775[/import]

Please help! I have been stuck on this phase of my app for sometime and I need some solid direction. [import]uid: 46629 topic_id: 9122 reply_id: 33934[/import]

@skylersb,

is your game a topview or sideview? [import]uid: 12455 topic_id: 9122 reply_id: 33942[/import]

My game is sideview landscape. [import]uid: 46629 topic_id: 9122 reply_id: 33949[/import]

So, you need the touch function, correct?

I recommend you check out the Samurai Kitchen sample here:

http://developer.anscamobile.com/code/samurai-kitchen

When I get home, I’ll try to post up some codes for you.

[import]uid: 12455 topic_id: 9122 reply_id: 33980[/import]

@ teex84

Thank you for your help bro. I’m still having trouble figuring it out but I’m making a little progress. But yes, I need the touch function so that I can move the turret from 0 to 90 degrees with my finger and then have it stop rotation at 0 and 90 degrees. Does that make sense?
[import]uid: 46629 topic_id: 9122 reply_id: 34117[/import]

Hey, sorry for the late reply. This is a very simple demo I just knocked up, is it what you are after?

http://dl.dropbox.com/u/571145/tank.zip

Drag your finger around to aim the “turret” and release to fire. [import]uid: 5833 topic_id: 9122 reply_id: 34120[/import]

No problem bro. That is exactly what I am looking for, great job man! I want to use and implement that and at the same time I want to make constraints to where you cant rotate upwards past 90 degrees and you cant rotate downwards past 0 degrees. Thanks again bro! [import]uid: 46629 topic_id: 9122 reply_id: 34122[/import]

No problem, constraining it should be pretty straight forward but if you have any issues just ask. [import]uid: 5833 topic_id: 9122 reply_id: 34124[/import]

For some reason my Corona Simulator keeps crashing. My previous coding might be clashing somewhere with what I implemented of your coding. Any ideas? The joys of learning to program :slight_smile: [import]uid: 46629 topic_id: 9122 reply_id: 34127[/import]

Are you getting any errors in the console? [import]uid: 5833 topic_id: 9122 reply_id: 34128[/import]

I fixed the problem of it crashing, however now I am trying to get the turret to pivot from the center of the tank so that the end of the turret cant be seen when you move the turret up and down when you shoot. For some reason, when I fire it shows the turret facing the opposite direction yet the bullet goes in the direction I fired. [import]uid: 46629 topic_id: 9122 reply_id: 34136[/import]

have you checked your turrent rotation? [import]uid: 12455 topic_id: 9122 reply_id: 34139[/import]

I have played around with the turret rotation and still haven’t found what is causing the problem. Thank you for helping me out a lot and being patient with me as I learn.

Here is some of my coding:
local balls = {}
local ballSpeed = 13

local turret = display.newImage( “subturret.png” )
turret.x = 146; turret.y = 530
turret.rotation = 30

local sub = display.newImage( “submarine.png” )
sub.x = 110; sub.y = 590
local fire = function()

local ball = display.newImage(“ball.png”)
ball.x = turret.x; ball.y = turret.y
physics.addBody( ball, { density = 15,friction=0.5, bounce=0.3, radius=20} )

ball.velocity = {}

ball.velocity.x = -math.cos( math.rad( turret.rotation - 90 ) )
ball.velocity.y = -math.sin( math.rad( turret.rotation - 90 ) )

balls[#balls + 1] = ball

end

local onUpdate = function( event )

for i = 1, #balls, 1 do
balls[i]:move( balls[i].velocity.x * ballSpeed, balls[i].velocity.y * ballSpeed )
end

end

local onTouch = function( event )

if event.phase == “began” or event.phase == “moved” then
local angle = math.angleBetween( event, sub )

turret.rotation = angle
else
fire()
end

end

Runtime:addEventListener( “enterFrame”, onUpdate )
Runtime:addEventListener( “touch”, onTouch )

Runtime:addEventListener( “enterFrame”, onUpdate ) [import]uid: 46629 topic_id: 9122 reply_id: 34154[/import]

this is the code I used to make my cannon game. see if it will work for your turrent
[lua] local shootTouch = function ( event )
if gameIsActive then

local x,y = cannongun:localToContent( cannongun.x - cannongun.xOrigin, cannongun.y - cannongun.yOrigin )
local dx = event.x - x
local dy = event.y - y
local angle = math.atan2( dy, dx )

if event.phase == “began” and event.xStart > 0 and event.xStart < 420 and event.yStart < 280 and event.yStart > 0 then

cannongun.xStart = event.x
cannongun.yStart = event.y
cannongun.angleStart = angle
cannongun.rotationStart = event.rotation

cannongun.xStart = cannongun.x
cannongun.yStart = cannongun.y
cannongun.angleStart = angle
cannongun.rotationStart = cannongun.rotation

elseif event.phase == “moved” and event.xStart > 0 and event.xStart < 420 and event.yStart < 280 and event.yStart > 0 then
local pi = math.pi

local delta = (angle - cannongun.angleStart)
delta = delta*180/pi

cannongun.rotation = cannongun.rotationStart + delta

if cannongun.rotation <= 90 then
cannongun.rotation = 90

elseif
cannongun.rotation >= 180 then
cannongun.rotation = 180
end

elseif event.phase == “ended” or event.phase == “cancelled” then

cannongun.rotation = cannongun.rotation

end
end
end

– active the touch with a Runtime

Runtime:addEventListener(“enterFrame”, shootTouch)

– and you’re done [import]uid: 12455 topic_id: 9122 reply_id: 34345[/import]

Hey there

How would you implement images into that sample?
It’s not working out for me so far. [import]uid: 54001 topic_id: 9122 reply_id: 41079[/import]