[Physics] Question on using body:applyForce for a bullet.

Forgive me if I am missing the obvious in the API docs. Suppose I have a ‘turret’ sprite which I created using the beebe games class, and from that I can calculate the angle from my turret to another point on screen. From thsi I can successfully track another sprite on screen.

Is there a handy function to calculate the xForce and yForce values required to shoot a bullet from my turret at the angled calculated by the getAngleTo() method from the beebegames class? [import]uid: 11672 topic_id: 4798 reply_id: 304798[/import]

Dang, thought I had commented on the previous post which was very much along these lines, but anyway…

Here’s a link I nabbed from the other post someone was asking about turret shooting “tower defense” style games:

Flash demo: http://paul.sc/shooting-moving-target/
PDF: http://paul.sc/shooting-moving-target/Shooting_Moving_Target.pdf

And I’ve taken the flash code from the PDF and converted it to lua, though please note I’ve only tested it here: http://www.lua.org/cgi-bin/demo and not in an actual program:

[lua]function newBall(x,y,vx,vy)
local ball = {}
ball.x, ball.y = x, y
ball.vx, ball.vy = vx, vy
return ball
end

local p = newBall(10,10,10,10)
local q = newBall(100,100,10,10)

local v = 10
local bulletSpeed = 50

local dx, dy = p.x - q.x, p.y - q.y
local a = p.vx * p.vx + p.vy * p.vy - v * v
local b = 2 * (p.vx * dx + p.vy * dy )
local c = dx * dx + dy * dy

– Check we 're not breaking into complex numbers
local r = b * b - 4 * a * c

if (r < 0) then
– Doesn 't look like this is going to work !
print( " Shit !" )
end

– The time that we will hit the target
if (a < 0) then
a = -1
else
a = 1
end

local t = (a * math.sqrt( r ) - b) / (2 * a)

– Aim for where the target will be after time t
dx = dx + t * p.vx
dy = dy + t * p.vy

local theta = math.atan2 (dy , dx )

– Throw the ball
q.vx = bulletSpeed * math.cos( theta )
q.vy = bulletSpeed * math.sin( theta )[/lua]

Let us know how you get on…

matt. [import]uid: 8271 topic_id: 4798 reply_id: 15419[/import]

Ok, I knew this would happen - here’s the link to the earlier post:

http://developer.anscamobile.com/forum/2010/12/06/tower-defense-physics

Is this helpful? I think @jmp909’s ideas are pretty much on point for what you want, which might eliminate the need for the physics engine, as well, in your case.

matt [import]uid: 8271 topic_id: 4798 reply_id: 15421[/import]

I’m at work at the moment, will try this out when I get home. Very much appreciated Horace.

As a follow up question, I’m using the physics engine for more than just bullet/object collision, but nothing so intensive that it can’t be replaced with a smaller section of my own code if I took the time to code it. Is there a big overhead with using what may essentially be a small subset of the physics code?

Bit of a ‘how long is a piece of string’ question I know, but your comment on eliminating the need for the physics engine raised a red flag for me.

[import]uid: 11672 topic_id: 4798 reply_id: 15439[/import]

I believe (as I’m not part of Ansca) that the only overhead performance-wise when using the physics engine is how many objects you use it for. This is why things like Particle Candy are good - they use different algorithms for heavy calculations scenarios.

If you only use say 3 physics objects, the ‘overhead’ will be low. If you’re talking about ‘app size’ or ‘footprint’ then simply using the physics library increases the build output of app. Check the docs for how much as it’s not something I worry about.

matt. [import]uid: 8271 topic_id: 4798 reply_id: 15443[/import]

Btw, quick cross-post to let everyone know that the original site for the PDF tutorial is no longer there:

http://developer.anscamobile.com/forum/2011/03/22/animating-moving-my-enemies#comment-29132 [import]uid: 8271 topic_id: 4798 reply_id: 29133[/import]