Help with shooting bullets

Ok I can't figure out how to make a bullet luanch out of my moving object,
my object in the code is a simple red square, controlled by a simple joystick using the lib_analog_stick.lua file and i modified it a bit to use my own images for the joystick, i've looked all around and i still can't find a solution to my problem

p.s.
I deleted any try at shooting, i need a code or some advice on how to shoot with a joystick-controlled player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
display.setStatusBar( display.HiddenStatusBar )
 
        local physics = require( "physics")
 
physics.start()
physics.setGravity( 0, 0 )
 
system.activate( "multitouch" )
 
 
 
local background = display.newImage("gameb.png")
background.x = 160
background.y = 240
 
local button = display.newImage("button.png")
button.x = 40
button.y = 420
 
 
 
 
 
background.rotation = 90
local StickLib   = require("lib_analog_stick")
 
local screenW = display.contentWidth
local screenH = display.contentHeight
local Text        = display.newText( " ", screenW*.6, screenH-20, native.systemFont, 15 )
 
-- CREATE A SPACE SHIP
local Ship      = display.newImage("red.png")
Ship.x = 160
Ship.y = 240
Ship.xScale = .5
Ship.yScale = .5
physics.addBody( Ship)
 
 
 
-- CREATE ANALOG STICK
MyStick = StickLib.NewStick( 
        {
        borderImage   ="joystickb.png",
        thumbImage    ="joystickt.png",
        x             = 30,
        y             = 60,
        thumbSize     = 16,
        borderSize    = 32, 
        snapBackSpeed = .6, 
       } )
 

create a function that displays a new image (bullet) with the below. so it will look as if the bullet originated from where your ship is at the moment.

1
2
bullet.x = ship.x +  10 -- or whatever will look good with your ship
bullet.y = ship.y + 10 -- or whatever will look good with your ship

Excellent answer nemensiris! (I don't know whether or not you prefer I use your forum name or first name so am sticking with forum for now.)

Peach :)

Hey Peach! thanks, I try! I don't mind either or, although my first name might be easier... I think it's actually taken as a username :P

Probably - I know about 4 Corona devs with the same name.

To the best of my knowledge there are only two "peach" accounts - and both are mine ;)

ok i did what you said but it only displays on the bottom most side and not on the front where the square is pointing, also how would i go about shooting the bullet in the direction the player is "pointing".

Thanks for the quick replies.

did you try adjusting the "+10" values to both x and y? You can change the "10" to whatever value or take place a "-" instead of "+" to achieve the desired location.

once you have a function that displays/spawns a bullet in the desired location then you'll need to work in physics, velocity into that function so that way when you fire your bullet it will "fly" in the direction you want. In my case I have something like this;

1
2
physics.addBody(bullet, {bounce=0.0, isSensor=true})
bullet:setLinearVelocity(150, 0)

Thanks for the quick reply,

I tried it with the code you suggested and just set the bullet.x = Ship.x and same with the y cords to make it spawn in the middle, and one problem is that the bullets only shoot right, and i need it to shoot in the direction that it is pointing I know how to adjust the linear velocity to shoot in other directions i just need help on getting it to shoot in the direction the square is facing,

by the way, my game is not a bottom-up shooter where it only needs to shoot up.

Thanks for all the help.

did you try adjusting the other value in the setLinearVelocity?

in my case I have a bullet "shooting" towards the right of the screen with

1
bullet:setLinearVelocity(400, 0)

@peach - maybe we can kick someone's screen name out make my first name my screen name. maybe I can bribe someone... you :)

i understand adjusting the x,y cord. to the linear velocity but is there some function or something to get what direction the front of the ship is pointing then using that to shoot a bullet straight into the direction of the ship?

thanks.

you're talking about like a turret? I did that before with the below

http://developer.anscamobile.com/forum/2011/05/11/collision-happens-outside-display-not-enemies

inside the code there you'll find the code you're looking for plus most of the collision components you'll need.

...is there some function or something to get what direction the front of the ship is pointing...

Unless there are outside forces affecting the ship, you can *always* know what direction the ship is facing because your code turns the ship, right?

So if the player uses the joystick to turn the ship to the left, right, up, or down, just save that info for later use.

Jay

Ok let's say my object is facing 30% (if up is 0 and down is 180)
then what kind of a code would make it so the linear Velocity is set so that the bullet comes out of the Ship at the same place the Ship is "pointing" in a straight line

bullet:setLinearVelocity(x,y)
(0,1)
(-1,0)(1,0)
(0,-1)

The above diagram thingy should help a little visualizing it. To shoot the bullet up you apply a positive linear velocity along the Y axis(0,1), to shoot down apply a negative linear velocity along the Y axis(0,-1) etc for left and right except linear velocity is along the X axis.

Using this principle you can shoot at 45 degrees by setting a positive linear velocity along BOTH the X and Y axis (1,1). Now using the joystick code you can use the rotation of the ship to set the needed linear velocity.

@nemensiris - I am afraid I can't take bribes, although I hear that Carlos is a shady fellow ;)

@Cheveol - that is a very simple but excellent example; a wonderful way of explaining things.

Peach :)

Thanks Peach

You can use a unit vector to calculate your velocity. Let's say your angle is 30 degrees:

T = total velocity you want the bullet to travel
Vx = cos(30) * T
Vy = sin(30) * T

The only special cases are when the angle is 0, 90 or 180

If your angle is 0 then you would use:

Vx = 0
Vy = T

If your angle is 90 then you use use:

Vx = T
Vy = 0

and finally, if your angle is 180 you would use:

Vx = 0
Vy = -T

I think I have the math right. Hope this helps.b

-Mark

views:1989 update:2012/2/8 8:46:10
corona forums © 2003-2011