【问题标题】:Define coordinates with rotation (corona sdk)用旋转定义坐标(corona sdk)
【发布时间】:2014-01-18 23:22:22
【问题描述】:

我已经问过我的问题,有人回答了我,但即使我找不到答案。
所以这是我的问题,我正在制作一款基于射击游戏的游戏,我的射击游戏是中间的圆圈。我可以通过触摸屏幕轻松旋转它,并且通过一些数学运算,射击者会显示在我触摸的位置。 (带有 event.x 和 event.y。
我想将子弹射向我的射手所显示的方向。我知道我的射手的坐标,我知道射手的轮换。
答案是使用三角学,问题是,我如何使用三角学来找到我的坐标?
我想使用转换功能,因为我需要这些坐标。如果有人知道另一种在没有这些坐标和/或过渡功能的情况下使子弹行进的方法,那将是可爱的!
在此先感谢,
范尼克!
Image:

【问题讨论】:

    标签: coronasdk


    【解决方案1】:

    这并不理想,但也许它可以帮助您更多地了解 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} )
    

    【讨论】:

    • 非常感谢 Fayer 的快速回答!我找到了另一种方法,再次使用三角函数,我将发布答案!您的想法也有效,但有时它可能会因为我的按钮而滞后,如果我触摸拍摄按钮,它会将事件坐标更新为拍摄按钮之一,但如果我使用操纵杆而不是全屏它肯定会工作!非常感谢!
    • 不客气!请记住,感谢人们的方式是支持他们的回答:)
    • 是的,我知道,我想支持你的回答,但显然我需要 15 件事情来做到这一点 0.o
    【解决方案2】:

    编辑:我找到了为什么我没有找到与我的计算器相同的解决方案,math.tan 仅适用于弧度!这就是为什么它给了我一些完全随机的值。完成后我会与您分享代码!

    所以这是我找到的代码,我用一个 200pi x 200pi 的屏幕来做一个例子,
    “c”是我的射手,它在屏幕中间,所以 c.x = 100 和 c.y = 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
    

    如果我使用计算器执行此代码,代码实际上可以完美运行。
    但是,我不知道为什么,但 corona sdk 没有相同的结果...
    有人知道为什么吗?

    【讨论】:

    • 哦!我的错!我发现了问题,math.tan 仅适用于弧度角度! :) 我会分享你最终的代码 :)
    【解决方案3】:

    原来如此! :) 从中间的一个对象开始,随着对象以度为单位旋转,它将定义子弹将在过渡到的位置,注意“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
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2016-08-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多