how to fire object at a given angle?

Hi,

I have tried to find the answer to this before asking , but can’t seem to see it.

I have an object which rotates depending on the angle of the joystick. I want to fire a bullet from this object at the angle the object is at.

However, I don’t seem to be able to find a way to push the bullet in the correct direction.

I think I need a code to move the bullet in relation to its current angle or position, rather than the screen X, Y.

Any help appreciated!

Thanks [import]uid: 8699 topic_id: 8382 reply_id: 308382[/import]

This should do it:

  
local bullet = display.newCircle( display.contentWidth / 2, display.contentHeight / 2, 10 )  
bullet:setFillColor( 255, 0, 0 )  
  
local speed = 5  
bullet.rotation = 45  
  
local movementScale = {}  
movementScale.x = math.cos( math.rad( bullet.rotation - 90 ) )  
movementScale.y = math.sin( math.rad( bullet.rotation - 90 ) )  
  
local velocity = {}  
velocity.x = movementScale.x \* speed  
velocity.y = movementScale.y \* speed  
  
local onUpdate = function( event )  
  
 bullet.x = bullet.x + velocity.x  
 bullet.y = bullet.y + velocity.y  
  
end  
  
Runtime:addEventListener( "enterFrame", onUpdate )  
  

If you need to get the angle between two points, say a player and a target, then that is also easy to do and I have put the code required into Rum - http://www.monkeydeadstudios.com/rum.php?page=math [import]uid: 5833 topic_id: 8382 reply_id: 29985[/import]

Just playing with Rum now Graham, thanks for that!

I’ve been struggling with firing an object in a certain direction and I thinks it because I’m using physics.applyForce() is it feasible with that? The reason I ask is that the first two params of that function take an X and Y which to fire from an object that can rotate these values will change based on what direction the object is facing.

It would be easier (I think) if applyForce could just take an angle in which to fire and a velocity.

Gary [import]uid: 7334 topic_id: 8382 reply_id: 30005[/import]

You should be able to do something like this with the previous code:

  
bullet:applyForce( velocity.x, velocity.x, bullet.x, bullet.y )  
  

You would place this in the update function instead of the direct position setting code and you may need to constantly update the velocity values based on the bullet if it rotates all the time. [import]uid: 5833 topic_id: 8382 reply_id: 30010[/import]

thanks for the code, I had found another piece of code on the forum in the meantime, but yours works much better.

Thanks again! [import]uid: 8699 topic_id: 8382 reply_id: 30011[/import]

Awesome, glad it was helpful and you’re very welcome! [import]uid: 5833 topic_id: 8382 reply_id: 30014[/import]

Hmmm, tried that didnt work fires out only horizontally left and right, must be me, I’ll post up some code shortly perhaps someone can help.

I did find some other code here and again, it doenst work 100% (the code posted by jmp909) http://developer.anscamobile.com/forum/2011/01/22/fire-ragdoll-position-touch

Gary [import]uid: 7334 topic_id: 8382 reply_id: 30044[/import]

was me, got it working a treat now!

thanks again Graham for the Rum library. [import]uid: 7334 topic_id: 8382 reply_id: 30131[/import]

Awesome that it’s all working now! And glad you like Rum, if you have any suggestions or find any bugs please send me an email. [import]uid: 5833 topic_id: 8382 reply_id: 30154[/import]

. [import]uid: 46629 topic_id: 8382 reply_id: 34150[/import]

@GrahamRanson, where did you learn how to use math.cos, math.sin, etc in game logic? Your code works like a charm, but I want to be able to understand how to apply math.cos to future projects. [import]uid: 14218 topic_id: 8382 reply_id: 42999[/import]

@ spoggles I’d like to say it was learnt in math lessons in school however that’s just not true. When I was in school I never could understand why things like that would be useful. I was naive. If only they used games dev ( with Corona naturally ) in secondary school maths lessons I’d have been much more interested.

Basically just have a look around Google and you will find some tutorials etc on 2D games dev using trigonometry you should be good to go. [import]uid: 5833 topic_id: 8382 reply_id: 43005[/import]

I think, all aspiring game developers should learn some basic stuff, instead of bruteforcing their way when they hit a wall…

My advice -

  1. learn basic trigonometry
  2. learn basic physics and laws of motion
  3. learn basic of vector maths and physics
    [import]uid: 48521 topic_id: 8382 reply_id: 43006[/import]

I agree! I’ve gotten better now, this was when I was 15/16 and bored in maths lessons. I’ve since gone to college and Uni and got a better understanding of all that. Still by no means an expert but just enough to be dangerous :slight_smile: [import]uid: 5833 topic_id: 8382 reply_id: 43189[/import]

@GrahamRanson A lot of the tutorials I found use C++ as the programming language of choice. Instead of learning a new programming language, I just want to learn the principles of applying math to gave dev. Do you have anything you suggest? [import]uid: 14218 topic_id: 8382 reply_id: 43212[/import]

This might help, I have never used it myself but there seems to be alot on there - http://www.essentialmath.com/ [import]uid: 5833 topic_id: 8382 reply_id: 46305[/import]