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]
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]
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
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)