Working Out An Angle To Shoot Relevant To Click Not Player

In a function we have stumbled across, it determines the angle by a bunch of equations and such to fire towards that point however because we start the character in the middle of a 2560 * 720 dimensioned game so 1280 when firing he will only fire left. this is basically what happens

(1280, 360)

|   \       |

| <  o    |

|   /       |

however when we move the player to the left most point of the map such as this point

(400, 360)

|  \    ^   /     |

| <   o   >    |

|  /   v    \    |

| = edge of screen

\ or / = Directional shooting

< = left shooting

> = right shooting

^ = shooting up

v = shooting down

I’ve suspected it is because the function bases it’s point to be shot at from the point it is created in the game which is (0,0) causing it to completely work in the second example

<lua>

-----------SHOOTING------------------
    – local canShoot=true
    function angleOfPoint( pt )
local x, y = pt.x, pt.y
local radian = math.atan2(y,x)
local angle = radian*180/math.pi
if angle < 0 then angle = 360 + angle end
return angle
    end
    
    function angleBetweenPoints( a, b )
            local x, y = b.x - a.x, b.y - a.y
            return angleOfPoint( { x=x, y=y } )
    end
    
    function rotateTo( point, degrees )
            local x, y = point.x, point.y
    
            local theta = math.rad(degrees)
    
            local pt = {
                    x = x * math.cos(theta) - y * math.sin(theta),
                    y = x * math.sin(theta) + y * math.cos(theta)
            }
    
            return pt
    end
        
    function fireBullet(source, angle,x,y)
            local bullet = display.newRect( 0,0 , 10,10 )
            bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle
            bullet:setFillColor(0,0,0)
            bullet.isType=“bullet”
            physics.addBody(bullet,“dynamic”,bulletBodyElement)
            group:insert(bullet)
            – canShoot=true

            local direction = rotateTo( {x=10,y=0}, angle )
            bullet:applyForce( direction.x, direction.y, bullet.x, bullet.y )
    end
    
    
    function canHeShoot()
        canShoot=true
    end
    – timer.performWithDelay(3000,canHeShoot,-1)
    
    function touch( e )
        – if isBuilding==false then
            local angle = angleBetweenPoints( guy, e )
            guy.rotation = angle+90
                – print(e.x,e.y)            
            
            if (e.phase == “began”) then
            elseif (e.phase == “moved”) then
            else
                – if canShoot then
                    fireBullet(guy, angle,e.x,e.y)
                – end
            end            
            return true
        – end
    end
    
    Runtime:addEventListener(“touch”,touch)

</lua>

Since you’re getting the screen’s coordinates of the touch event, you need to either map the touch event to the guy’s world coordinates, or convert the guy’s position to the corresponding screen coordinates in order to get the proper angle of trajectory.  Here’s how to convert the guy’s coordinates to the screen’s and get the angle accordingly:

local function angleBetweenCoords( sX, sY, dX, dY ) local xDist, yDist = dX - sX, dY - sY local angleBetween = math.deg( math.atan( yDist / xDist ) ) if sX \< dX then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end return angleBetween - 90 end (...inside touch function...) local gsx, gsy = guy:localToContent( 0, 0 ) local angle = angleBetweenCoords( gsx, gsy, e.x, e.y ) (...continue function...)

thank you for the speedy reply BeyondTheTech,

however shooting to the left of the player, goes as follows:

shooting -x, +y  =  +x, +y

shooting -x, -y  =  -x, +y

apart from that shooting right at any point on the screen is perfect!

just before you read that.

figured out by changing your angleBetweenCoords to this

<lua>

local function angleBetweenCoords( sX, sY, dX, dY )
local xDist, yDist = dX - sX, dY - sY
local angleBetween = math.deg( math.atan( yDist / xDist ) )
if sX < dX then angleBetween = angleBetween + 180 else angleBetween = angleBetween - 90 end
return angleBetween + 180
end

</lua>

all is fine

Since you’re getting the screen’s coordinates of the touch event, you need to either map the touch event to the guy’s world coordinates, or convert the guy’s position to the corresponding screen coordinates in order to get the proper angle of trajectory.  Here’s how to convert the guy’s coordinates to the screen’s and get the angle accordingly:

local function angleBetweenCoords( sX, sY, dX, dY ) local xDist, yDist = dX - sX, dY - sY local angleBetween = math.deg( math.atan( yDist / xDist ) ) if sX \< dX then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end return angleBetween - 90 end (...inside touch function...) local gsx, gsy = guy:localToContent( 0, 0 ) local angle = angleBetweenCoords( gsx, gsy, e.x, e.y ) (...continue function...)

thank you for the speedy reply BeyondTheTech,

however shooting to the left of the player, goes as follows:

shooting -x, +y  =  +x, +y

shooting -x, -y  =  -x, +y

apart from that shooting right at any point on the screen is perfect!

just before you read that.

figured out by changing your angleBetweenCoords to this

<lua>

local function angleBetweenCoords( sX, sY, dX, dY )
local xDist, yDist = dX - sX, dY - sY
local angleBetween = math.deg( math.atan( yDist / xDist ) )
if sX < dX then angleBetween = angleBetween + 180 else angleBetween = angleBetween - 90 end
return angleBetween + 180
end

</lua>

all is fine