object rotation and applyForce

Hi,

probably me but Ive got a strange little problem I cant seem to solve.

Ive got a gun like a tank which fires a bullet in the direction of the touch on the screen.

I am using the angleBetween function which I found on this site. Now If I set the object.rotation to the result of anglebetween its spot on. However to fire my bullet I use an applyForce(angleBetween, yspeed, bullet.x, bullet.y) but it doesn’t track acurately.

For example if the tank is facing west (not sure if thats zero degrees) the bullets are flying out at about 30 degrees. As I move around towards 90 degrees (tank facing north) the accuracy improves but then starts to drop off again as I move towards 180 degrees. Again with the tank facing west now the bullets are firing at about 150 degrees. I’m not sure why???
TIA

Gary [import]uid: 7334 topic_id: 6361 reply_id: 306361[/import]

Hmm sounds like a math equation situation. I had a similar issue, but I solved it. If you post some code. I’ll see if it’s similar to mine and I’ll try to post a solution. [import]uid: 12455 topic_id: 6361 reply_id: 21994[/import]

Hi Yellow,

Sure its like this;

 local bearingSpeed = math.random(MIN\_SPEED, MAX\_SPEED) \* - 1  
  
 local xDist = theTouchX - disk.x ; local yDist = theTouchY - disk.y  
 local angleBetween = math.deg( math.atan( yDist/xDist ) )  
 if ( disk.x \< theTouchX ) then angleBetween = angleBetween+90 else angleBetween = angleBetween-90 end  
  
 blueGun.rotation = angleBetween  
 disk:applyForce( angleBetween, bearingSpeed, disk.x, disk.y )  

disk.x is display.contentWidth / 2 and disk.y is 10 i.e. in the middle of the screen at the bottom.

blueGun is just an image and that works fine, rotates exactly to where I want. The issue is firing the bullet (disk) bearingSpeed is between 50 and 100

Thanks
Gary
[import]uid: 7334 topic_id: 6361 reply_id: 22171[/import]

Anyone help please?

Cheers
Gary [import]uid: 7334 topic_id: 6361 reply_id: 22679[/import]

Hey Gary,

srry I haven’t been in here for a few days. Work related stuff.

I post my code on this forum. I hope it helps.

http://developer.anscamobile.com/forum/2011/02/12/making-cannon [import]uid: 12455 topic_id: 6361 reply_id: 23190[/import]

Hi yb

I appreciate you taking the time to come back to me. I tried your code and exactly the same thing happens, seems I cant fire a bullet less than 45 degrees or more than 135 degress

This is best explained through a video so I uploaded one here

http://www.youtube.com/watch?v=WMBQVxTsKE0
Cheers
Gary [import]uid: 7334 topic_id: 6361 reply_id: 23416[/import]

hey Gary. I came up with a solution that I think it might work.
It might not be the best solution, but it might work.

[lua]local R = 5

local yForce
local xForce = R - (((180 - cannongun.rotation)/90) * R)
local yForce1 = R * ((cannongun.rotation - 180)/180) * 2
local yForce2 = R * ((((cannongun.rotation + 180)/180) - 1) * -2)

if cannongun.rotation >= 90 then

yForce = yForce1

elseif cannongun.rotation < 90 then

yForce = yForce2
end

steelball:applyLinearImpulse( xForce, yForce, steelball.x, steelball.y)
steelball.collision = onDragonCollision
steelball:addEventListener(“collision”, steelball) [import]uid: 12455 topic_id: 6361 reply_id: 23428[/import]

Hey yellow,

appreciate you taking the time out. That actually makes things worse, it seems to be either 45 or more degrees out all the time.

I need to go back to the drawing board completely me-thinks.

Cheers

Gary [import]uid: 7334 topic_id: 6361 reply_id: 23867[/import]