2014-01-14 32 views
0

我已經問過我的問題,有人回答我,但即使我找不到答案。
所以這裏是我的問題,我正在製作一款基於射手的遊戲,我的射手是中間的圓圈。我可以很容易地旋轉它,通過觸摸屏幕,並用一些數學,射手顯示在我觸摸的點。 (與event.x和event.y
我想射擊子彈的方向,我的射手顯示。我知道我的射手的座標,我知道射手輪換
而答案是要使用三角函數,問題是,如何使用三角函數來查找我的座標?
我想使用過渡函數,因爲我需要這些座標。如果有人知道另一種方法讓子彈在沒有這些座標的情況下行進座標和/或過渡功能,這將是可愛的!
在此先感謝,
Fannick!
Image:定義旋轉座標(電暈sdk)

回答

0

他就是這樣! :)從中間的一個物體開始,隨着物體的旋轉度數,它將定義子彈在過渡到的位置,請注意「c」是射手,「h」是display.contentHeight和「 w「display.contentWidth。我還將bulletX和bulletY定義爲子彈的第二個位置。

if c.rotation <= math.deg(math.atan((h/2)/(w/2))) and c.rotation >= 0 then 
    bulletX = w 
    bulletY = h/2 + w/2 * math.tan((c.rotation * math.pi)/180) 

elseif c.rotation > math.tan(math.rad((h/2)/(w/2))) and c.rotation <= 90 then 

    bulletX = w/2 + math.tan((90 * math.pi)/180 - (c.rotation * math.pi)/180) * h/2 
    bulletY = h 

elseif c.rotation > 90 and c.rotation <= 90 + math.deg(math.atan((w/2)/(h/2))) then 

    bulletX = w/2 - math.tan(((c.rotation * math.pi)/180) - ((90 * math.pi)/180)) * h/2 
    bulletY = h 

elseif c.rotation > 90 + math.deg(math.atan((w/2)/(h/2))) and c.rotation <= 180 then 
    bulletX = 0 
    bulletY = h/2 + math.tan(math.pi - ((c.rotation*math.pi)/180)) * w/2 

elseif c.rotation > 180 and c.rotation <= 180 + math.deg(math.atan ((h/2)/(w/2))) then 
    bulletX = 0 
    bulletY = h/2 - math.tan(((c.rotation * math.pi)/180) - math.pi) * w/2 

elseif c.rotation > 180 + math.deg(math.atan ((h/2)/(w/2))) and c.rotation <= 270 then 
    bulletX = w/2 - math.tan(((270 * math.pi)/180) - ((c.rotation * math.pi)/180)) * h/2 
    bulletY = 0 

elseif c.rotation > 270 and c.rotation <= 270 + math.deg(math.atan((w/2)/(h/2))) then 
    bulletX = w/2 + math.tan(((c.rotation * math.pi)/180) - ((270 * math.pi)/180)) * h/2 
    bulletY = 0 

elseif c.rotation > 270 + math.deg(math.atan((w/2)/(h/2))) and c.rotation <= 360 then 
    bulletX = w 
    bulletY = h/2 - math.tan(((360 * math.pi)/180) - ((c.rotation * math.pi)/180)) * w/2 
end 
0

這是不理想,但也許它可以幫助您瞭解更多關於CoronaSDK和你想做些什麼。

local touchedX -- the X coordinate of the touch 
local touchedY -- the Y coordinate of the touch 
local shooterX -- the X coordinate of the shooter 
local shooterY -- the Y coordinate of the shooter 

local bullet = display.newImage("bullet.png") -- the image for the bullet 
bullet.x = shooterX 
bullet.y = shooterY 

--this is what is going to move the bullet from the shooter to the place you touched 
transition.to(bullet, {time = 100, x = touchedX, y = touchedY}) 
+0

非常感謝您爲Fayer快速回答!我發現了另一種方法來做到這一點,再次用三角函數,我會發布答案!你的想法也適用,但有時候可能因爲我的按鈕而落後於我,如果我觸摸拍攝按鈕,它會將事件座標更新爲拍攝按鈕之一,但如果我使用操縱桿而不是全屏幕,肯定會工作!非常感謝! – Fannick

+0

你好!請記住,感謝人們的方式是upvote他們的答案:) – Fayer

+0

是的,我知道它,我想upvote你的答案,但顯然我需要15 thingy做到這一點0.o – Fannick

0

編輯:我發現我爲什麼不找同一個解決方案,我的計算器,math.tan只與弧度的工作!這就是爲什麼它給了我一些完全隨機的價值​​。一旦完成,我將與您分享代碼!

所以這是我發現的代碼,我使用200pi 200pi的屏幕做出了榜樣,
「c」是我的射手,這是在屏幕的中間,所以CX = 100和cy = 100。如上所述,我可以讓射手參加一項賽事。

if c.rotation <= 45 and c.rotation >= 0 then 
    bulletX = 200 
    bulletY = 100 + math.tan(c.rotation) * 100 
end 
if c.rotation > 45 and c.rotation <= 90 then 
    bulletX = 100 + math.tan(90 - c.rotation) * 100 
    bulletY = 200 
end 
    if c.rotation > 90 and c.rotation <= 135 then 
    bulletX = 100 - math.tan(c.rotation - 90) * 100 
    bulletY = 200 
end 
    if c.rotation > 135 and c.rotation <= 180 then 
    bulletX = 0 
    bulletY = 100 + math.tan(180 - c.rotation) * 100 
end 
    if c.rotation > 180 and c.rotation <= 225 then 
    bulletX = 0 
    bulletY = 100 - math.tan(c.rotation - 180) * 100 
end 
    if c.rotation > 225 and c.rotation <= 270 then 
    bulletX = 100 - math.tan(270 - c.rotation) * 100 
    bulletY = 0 
end 
    if c.rotation > 270 and c.rotation <= 315 then 
    bulletX = 100 + math.tan(c.rotation - 270) * 100 
    bulletY = 200 
end 
    if c.rotation > 315 and c.rotation <= 360 then 
    bulletX = 200 
    bulletY = 100 - math.tan(360 - c.rotation) * 100 
end 

如果我用我的計算器進行操作,代碼實際上是完美的。
但是,我不是爲什麼,但電暈sdk有不一樣的結果...
有沒有人有一個想法,爲什麼?

+0

哦!我的錯!我發現這個問題,math.tan只適用於以弧度表示的角度! :)我會分享你最終的代碼:) – Fannick