bullet movement

I am looking for a way to send 'bullets' to a point touched on the screen without using transition.to.

When using transition.to the bullets stop at the event.x and event.y, but I want them to keep moving off of the screen after having passed through the coordinates selected by the user.

I would imagine this is a math problem, but either way any help would be appreciated.

You can just use coordinates to move the bullet instead. For example, in a runtime listener keep setting the x coordinate to increase by a certain amount.

That does work to move the bullet. Thanks for your response.

Unfortunately I can't figure out how to keep moving the bullet in a constant direction after it passes through the original selection. This reminds me of the formulas for linear equations but its been years since I took geometry and calc. :)

Thanks again.

Hmmm... what if you multiply the x and y by a certain factor? As long as you multiply them by the same number it will keep the same trajectory. Just an idea off the top of my head.

For example, say you want the bullet to travel to 100, 150. Multiply that by say 4 and it will travel to 400, 600 which will be off the screen. It will still pass through 100, 150 but will continue going. Again just a thought and there may be a better way of doing it.

Shouldn't it work if you make the bullet a sensor? It'll then read the collision, and pass through. Just a thought

Thanks for all the help.

Multiplying by a number does make the bullet continue moving if I include a second transition, however it makes the object continue in a new path at an angle from the old one. Even if I use if/thens to break the screen into quadrants, the movement is close to the original path but still not exact and looks awkward.

Making the object a sensor doesn't help because it still just stops wherever the transition coordinates are set.

I would imagine this could be done with applyForce, or applyLinearImpulse, but I can't seem to find an equation or solution for that method either.

@chaiHu

My original thought was not to use a 2nd transition, but to make the original transition move to the tap location multiplied by the multiplication factor. Maybe thats why it's going in a different direction? For example if they tap at 100, 150 you would make the transition go to x*4 and y*4.

If that doesn't work, do you have any code you can post?

I got it.

The way to make the bullet continue in a straight line is to find the difference between the starting x and y and the ending x and y. You then add this difference to the x and y to get the next point on the line.

Not sure why that took me so long. Like I said, it's been a while since my last math class.

Thanks for all your help!

Oh, and here is the code in case you wanted it:

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
local background = display.newRect (0, 0, 320, 480)
background.alpha = 0.1
 
local machineGunX = 0
local machineGunY = 0
 
local playerObject = display.newCircle (160, 240, 20)
 
local function machineGun(event)
        if event.x > playerObject.x + 60 then
                machineGunX = machineGunX + event.x
                machineGunX = event.x - playerObject.x
                machineGunY = event.y - playerObject.y
                local machineGunB = display.newCircle (playerObject.x, playerObject.y, 5)
                transition.to(machineGunB, {time=800, x=event.x + machineGunX*6, y=event.y + machineGunY*6})
        end
        if event.x < playerObject.x - 60 then
                machineGunX = machineGunX + event.x
                machineGunX = event.x - playerObject.x
                machineGunY = event.y - playerObject.y
                local machineGunB = display.newCircle (playerObject.x, playerObject.y, 5)
                transition.to(machineGunB, {time=800, x=event.x + machineGunX*6, y=event.y + machineGunY*6})
        end
        if event.y < playerObject.y - 60 then
                machineGunX = machineGunX + event.x
                machineGunX = event.x - playerObject.x
                machineGunY = event.y - playerObject.y
                local machineGunB = display.newCircle (playerObject.x, playerObject.y, 5)
                transition.to(machineGunB, {time=800, x=event.x + machineGunX*6, y=event.y + machineGunY*6})
        end
                if event.y > playerObject.y + 60 then
                machineGunX = machineGunX + event.x
                machineGunX = event.x - playerObject.x
                machineGunY = event.y - playerObject.y
                local machineGunB = display.newCircle (playerObject.x, playerObject.y, 5)
                transition.to(machineGunB, {time=800, x=event.x + machineGunX*6, y=event.y + machineGunY*6})
        end
end
 
background:addEventListener("tap", machineGun)
views:1523 update:2011/11/26 9:01:35
corona forums © 2003-2011