2014-01-12 142 views
0

我正在制作一款基于射手的游戏。 我的射手是在屏幕的中间,我可以接触与event.x,event.y屏幕时,移动射击的旋转,但我的问题: 我想打一个按钮,从拍摄的子弹射手朝着射手所展现的方向发展。 我知道我可以用三角函数解决这个问题,但我正在寻找一种更简单的方法。 任何人都知道如何做一个轮换过渡? 谢谢 Fannick自转球(转换)

+0

你有什么累了这么远吗? – agconti

+0

好了,现在什么都没有,我知道这可能与三角做,我只是想辩别如何解决这个问题,我imaginated创建一个无形的圈子,和方向的射手正显示出将被显示为点在圈子上做转型。 (圆实际上离开了屏幕,所以从中心开始,以半径250为例) – Fannick

回答

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 
1

老实说,我不知道现在如果有可能拉这一关没有三角(和我有急事,所以我真的没有想到一上来的时候),但即使我非常怀疑这会使这个过程变得更容易。

“罪”和“COS”是两个功能似乎相当吓人的初学者,尤其是在这样的背景下,但坦率地说,他们是相当温顺。你可以做的最好的事情是上网搜索一些三角函数的解释(一些大学教授发布他们的讲座文档,并且通常对刚刚开始学习的初学者做出解释),你会得到挂起它很快。因为知道如何将这些知识应用于进一步的问题后来可以证明是非常有用的,找到一个不需要使用三角法的“解决方法”是不值得的。

0

您可以使用电晕物理模块。它可能会更容易,如果您也需要它,甚至可以轻松实现碰撞检测。 看一看这些链接:

随着物理引擎,你只需要重力设置为0,你的子弹添加到与addBody的物理引擎()然后对子弹施加冲动,使其沿着你想要的方向(你的目标)移动applyLinearImpulse()。

+0

非常棒! :) 谢谢! – Fannick