Can't shoot to the right direction

I have a object that is always rotating with a cannon. I want to shoot a bullet, but when I do, it goes always to the right direction instead of the cannon direction. I tried to offset the bullet. What I am doing wrong?

local function go(event)
    display.remove(logo)
    display.remove(begin)
    local planet = display.newImage(“images/earth.png”)
    planet.x = centerX
    planet.y = centerY
    – Esta função faz a terra rodar e aumento a sua rotação por cada 10 segundos
    local function animate()
        planet.rotation = planet.rotation + 2
        earthRotation = timer.performWithDelay( 10000, animate)
    end
    Runtime:addEventListener(“enterFrame”,animate);
    planet:scale(4, 4)
    transition.to( planet, { time = 200, xScale = 1, yScale = 1, onComplete = animate} )
    startGame()
    timerGame()
    timeCount()
    local function firebullet()
        local bullet = display.newCircle( planet.x, planet.y, 2 )
        physics.addBody( bullet, { density=3.0, friction=0.5, bounce=0.05 } )
        bullet.isBullet = true
        function getBulletOffset(planet, offsetAngle, offsetDist )
            local offset = math2d.angle2Vector( planet.rotation + offsetAngle, true )
            offset = math2d.scale( offset, offsetDist )
        return planet.x + offset.x, planet.y + offset.y
        end
        local bulletStartX, bulletStartY = getBulletOffset( planet, 90, 15 )
        bullet.angularVelocity = 50
        bullet:applyForce( 100, 0, bullet.x, bullet.y )
        local bulletStartX, bulletStartY = getBulletOffset( planet, 90, 15 )
        
    end
    bg:addEventListener ( “tap”, firebullet)
    end
    begin:addEventListener ( “tap”, go)

Hi @ruimika90,

The reason your bullet is always going to the right is in how you’ve typed this line:

[lua]

bullet:applyForce( 100, 0, bullet.x, bullet.y )

[/lua]

That is basically applying a force in the “x” direction of 100, every time, so the bullet just goes right. Instead of using “100, 0” for the first 2 arguments, you’ll need to get the proper vector values from the “math2d” library, and tell Corona to apply the proper force in both the “x” and “y” directions.

Hope this helps,

Brent

Hi @BrentSorrentino,

Is there any way to get the direction to where the cannon (while rotating) is pointing when the user tap the screen, and then use a vector to fire the bullet in that direction?

Thx for the help,

Rui

Hi ruimika90. You’re missing a few essentials, as BrentSorrentino said, you are essentially telling it to apply ‘100’ force in the x direction, and no force in the y.

I suggest having a read about touch and tap events, and how you can pick up the coordinates of a touch.

An example of what you’ll need to make it work below -

[lua]

local function firebullet(event)

–Find difference between planet to the touch coordinates

local deltax = planet.x - event.x

local deltay = planet.y - event.y

–Find angle between planet and touch (in radians)

local bulletangle = math.atan2(deltay, deltax)

–Apply a force of ‘100’ at the angle between planet and touch coordinates

local xforce = (math.cos(angle))*100

local yforce = (math.sin(angle))*100

– Instead of this - bullet:applyForce( 100, 0, bullet.x, bullet.y )

bullet:applyForce( xforce, yforce, bullet.x, bullet.y )

[/lua]

I hope this helps, this is my first attempt at helping somebody with Corona :slight_smile:

Hi @frasderp,

Thx for the code and for the help, but the code is giving an error “bad argument 1# to cos(number expected, got nil)”. Any idea of what is causing this error and how to solve it?

Thx Again,

Rui

Apologies, I used the wrong reference to angle - 

Replace this section with the following.

[lua]

local xforce = (math.cos(bulletangle))*100

local yforce = (math.sin(bulletangle))*100

[/lua]

Ok, the error is gone, but what the planet is doing now, is shooting to the opposite direction of where you tap. For example, if I tap in the left of the planet, it will shoot to the right. What I really want to try, is, when we tap, it will shoot to the direction where the cannon is pointing while rotating.

Again, thx for all the help.

Change this part :slight_smile:

[lua]

–change this

local deltax = planet.x - event.x
local deltay = planet.y - event.y

–to this

local deltax = event.x - planet.x
local deltay = event.y - planet.y

[/lua]

Hi @ruimika90,

The reason your bullet is always going to the right is in how you’ve typed this line:

[lua]

bullet:applyForce( 100, 0, bullet.x, bullet.y )

[/lua]

That is basically applying a force in the “x” direction of 100, every time, so the bullet just goes right. Instead of using “100, 0” for the first 2 arguments, you’ll need to get the proper vector values from the “math2d” library, and tell Corona to apply the proper force in both the “x” and “y” directions.

Hope this helps,

Brent

Hi @BrentSorrentino,

Is there any way to get the direction to where the cannon (while rotating) is pointing when the user tap the screen, and then use a vector to fire the bullet in that direction?

Thx for the help,

Rui

Hi ruimika90. You’re missing a few essentials, as BrentSorrentino said, you are essentially telling it to apply ‘100’ force in the x direction, and no force in the y.

I suggest having a read about touch and tap events, and how you can pick up the coordinates of a touch.

An example of what you’ll need to make it work below -

[lua]

local function firebullet(event)

–Find difference between planet to the touch coordinates

local deltax = planet.x - event.x

local deltay = planet.y - event.y

–Find angle between planet and touch (in radians)

local bulletangle = math.atan2(deltay, deltax)

–Apply a force of ‘100’ at the angle between planet and touch coordinates

local xforce = (math.cos(angle))*100

local yforce = (math.sin(angle))*100

– Instead of this - bullet:applyForce( 100, 0, bullet.x, bullet.y )

bullet:applyForce( xforce, yforce, bullet.x, bullet.y )

[/lua]

I hope this helps, this is my first attempt at helping somebody with Corona :slight_smile:

Hi @frasderp,

Thx for the code and for the help, but the code is giving an error “bad argument 1# to cos(number expected, got nil)”. Any idea of what is causing this error and how to solve it?

Thx Again,

Rui

Apologies, I used the wrong reference to angle - 

Replace this section with the following.

[lua]

local xforce = (math.cos(bulletangle))*100

local yforce = (math.sin(bulletangle))*100

[/lua]

Ok, the error is gone, but what the planet is doing now, is shooting to the opposite direction of where you tap. For example, if I tap in the left of the planet, it will shoot to the right. What I really want to try, is, when we tap, it will shoot to the direction where the cannon is pointing while rotating.

Again, thx for all the help.

Change this part :slight_smile:

[lua]

–change this

local deltax = planet.x - event.x
local deltay = planet.y - event.y

–to this

local deltax = event.x - planet.x
local deltay = event.y - planet.y

[/lua]