Shooting the bullet- towards an object-in the current direction of gun

In my game ,I have a gun which is attached to vehicle.Gun and vehicle are joined using a “WELD” joint.

I want my gun to shoot bullets in the direction that the gun now points to.

But gun shoots bullets in only one direction.

Here is my code

function fireBullet(event) local bullet=display.newRect(gun.x+20,gun.y,50,10) bullet:setFillColor( 0,0,0 ) camera:add(bullet,1,false) physics.addBody( bullet,{density=3.0, friction=0.5, bounce=0.05,isSensor=true ,isBullet=true}) bullet:applyLinearImpulse( 1000 , 0, gun.x,gun.y ) end
  1. How to shoot the bullets in the current direction of gun?
  2. How to shoot the bullets towards a particular object?

Thanks

You need to only apply basic trigonometry.
 

  1. Determine what speed you want the bullets to fly at (currently you are using 1000)

  2. Replaced that 1000 and 0 with variables for x and y, and then

  3. calculate the angle between the gun (or player) and where the gun is pointed (or what object you want to fire at) using math.atan2.

  4. Then, using the angle you just calculated, you’d solve x variable using cos(angle) and y variable using sin(angle), both multiplied by the bullet’s speed to fire the bullet to a specific direction.

Assuming that your bullet isn’t round or around 1x1 pixels, then you may also want to use the angle to rotate the bullet (for which you need to turn the angle from radian to degree).

I’m not providing you with code as you’ll run into a lot of trigonometry during game development and it’s better to learn the basics sooner rather than later. Good luck!

Here’s my code.It doesn’t work as expected.It doesn’t shoot in the direction of “touch” event

local speed=1000 local function shootBullet(event) if event.phase=="began" then local bullet=display.newRect(gun.x+20,gun.y,10,10) bullet:setFillColor( 0,0,0 ) camera:add(bullet,1,false) bullet.isFixedRotation=true deltaX = event.x - gun.x deltaY = event.y - gun.y angle = math.atan2( deltaY, deltaX )\*180/math.pi physics.addBody( bullet) bullet:setLinearVelocity( math.cos( angle )\*speed , math.sin( angle )\*speed ) end end Runtime:addEventListener("touch",shootBullet)

Please help I’m stuck :wacko:  :wacko:

You almost have it, but you turn the angle into degrees too soon. Corona uses radians and therefore you need to insert the angle in radians for that to work. If you want to rotate the bullet’s display object, then you’d need to turn the angle into degrees and use that to rotate the object.

However, you’ll also want to create those bullets some other way. Since you create a local bullet within the function, you’ll lose reference to it once the function ends and you’ll have a difficult time cleaning it up. If you allow the player to fire multiple bullets repeatedly, then consider using a table for the bullets.

I added the below code …still it doesn’t work as expected.Please correct the code.

deltaX = event.x - gun.x deltaY = event.y - gun.y angle = math.atan2( deltaY, deltaX )\*180 angle=math.rad( angle ) print( angle ) physics.addBody( bullet) bullet:setLinearVelocity( math.cos( angle )\*speed , math.sin( angle )\*speed )

If you want to make 2D shooters, you will need to learn vector math which involves learning basic trigonometry.

This is unavoidable.

At the least you will need to understand the concepts and how to use someone else’s vector library.

SSK2 --> Math2D

I also have facing and movement helpers specific to 2D shooter style games: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/

Example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/04/fireBullet.zip

PS - Find and watch some 2d vector math videos related to game making:

This is a great set: https://www.youtube.com/playlist?list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My

PPS - The example I provided  fires from object A to object B.

What you want to do is:

  1. Get the angle of the turret.

  2. Convert the angle to a unit vector

  3. Scale the vector by bullet velocity.

  4. Make the bullet

  5. optionally rotate the bullet to face same direction as turret

  6. set the bullet linear velocity using the vector x and y values

Bang! done

Thanks RG!

I am going to learn trignometry

Happy christhmas :D  :smiley:

I just got back from visiting family, so my response is a bit delayed and seems to not be needed anymore. Still, as I said, your code was otherwise correct but you unnecessarily turned the angle from radians to degrees.

Here’s your code with the transformation commented out (and that’s all you needed to do). You were so close, so don’t give up and keep on learning! :smiley:

 

local speed=1000 local function shootBullet(event) if event.phase=="began" then local bullet=display.newRect(gun.x+20,gun.y,10,10) bullet:setFillColor( 0,0,0 ) camera:add(bullet,1,false) bullet.isFixedRotation=true deltaX = event.x - gun.x deltaY = event.y - gun.y angle = math.atan2( deltaY, deltaX ) --\*180/math.pi (this turns radians to degrees and messed things up.) physics.addBody( bullet) bullet:setLinearVelocity( math.cos( angle )\*speed , math.sin( angle )\*speed ) end end Runtime:addEventListener("touch",shootBullet)

You need to only apply basic trigonometry.
 

  1. Determine what speed you want the bullets to fly at (currently you are using 1000)

  2. Replaced that 1000 and 0 with variables for x and y, and then

  3. calculate the angle between the gun (or player) and where the gun is pointed (or what object you want to fire at) using math.atan2.

  4. Then, using the angle you just calculated, you’d solve x variable using cos(angle) and y variable using sin(angle), both multiplied by the bullet’s speed to fire the bullet to a specific direction.

Assuming that your bullet isn’t round or around 1x1 pixels, then you may also want to use the angle to rotate the bullet (for which you need to turn the angle from radian to degree).

I’m not providing you with code as you’ll run into a lot of trigonometry during game development and it’s better to learn the basics sooner rather than later. Good luck!

Here’s my code.It doesn’t work as expected.It doesn’t shoot in the direction of “touch” event

local speed=1000 local function shootBullet(event) if event.phase=="began" then local bullet=display.newRect(gun.x+20,gun.y,10,10) bullet:setFillColor( 0,0,0 ) camera:add(bullet,1,false) bullet.isFixedRotation=true deltaX = event.x - gun.x deltaY = event.y - gun.y angle = math.atan2( deltaY, deltaX )\*180/math.pi physics.addBody( bullet) bullet:setLinearVelocity( math.cos( angle )\*speed , math.sin( angle )\*speed ) end end Runtime:addEventListener("touch",shootBullet)

Please help I’m stuck :wacko:  :wacko:

You almost have it, but you turn the angle into degrees too soon. Corona uses radians and therefore you need to insert the angle in radians for that to work. If you want to rotate the bullet’s display object, then you’d need to turn the angle into degrees and use that to rotate the object.

However, you’ll also want to create those bullets some other way. Since you create a local bullet within the function, you’ll lose reference to it once the function ends and you’ll have a difficult time cleaning it up. If you allow the player to fire multiple bullets repeatedly, then consider using a table for the bullets.

I added the below code …still it doesn’t work as expected.Please correct the code.

deltaX = event.x - gun.x deltaY = event.y - gun.y angle = math.atan2( deltaY, deltaX )\*180 angle=math.rad( angle ) print( angle ) physics.addBody( bullet) bullet:setLinearVelocity( math.cos( angle )\*speed , math.sin( angle )\*speed )

If you want to make 2D shooters, you will need to learn vector math which involves learning basic trigonometry.

This is unavoidable.

At the least you will need to understand the concepts and how to use someone else’s vector library.

SSK2 --> Math2D

I also have facing and movement helpers specific to 2D shooter style games: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/

Example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/04/fireBullet.zip

PS - Find and watch some 2d vector math videos related to game making:

This is a great set: https://www.youtube.com/playlist?list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My

PPS - The example I provided  fires from object A to object B.

What you want to do is:

  1. Get the angle of the turret.

  2. Convert the angle to a unit vector

  3. Scale the vector by bullet velocity.

  4. Make the bullet

  5. optionally rotate the bullet to face same direction as turret

  6. set the bullet linear velocity using the vector x and y values

Bang! done

Thanks RG!

I am going to learn trignometry

Happy christhmas :D  :smiley:

I just got back from visiting family, so my response is a bit delayed and seems to not be needed anymore. Still, as I said, your code was otherwise correct but you unnecessarily turned the angle from radians to degrees.

Here’s your code with the transformation commented out (and that’s all you needed to do). You were so close, so don’t give up and keep on learning! :smiley:

 

local speed=1000 local function shootBullet(event) if event.phase=="began" then local bullet=display.newRect(gun.x+20,gun.y,10,10) bullet:setFillColor( 0,0,0 ) camera:add(bullet,1,false) bullet.isFixedRotation=true deltaX = event.x - gun.x deltaY = event.y - gun.y angle = math.atan2( deltaY, deltaX ) --\*180/math.pi (this turns radians to degrees and messed things up.) physics.addBody( bullet) bullet:setLinearVelocity( math.cos( angle )\*speed , math.sin( angle )\*speed ) end end Runtime:addEventListener("touch",shootBullet)