Calculate x/y from a certain point??

ilan

Expert
Licensed User
Longtime User
hi

i have a little issue.

i need to calculate the x/y of a certain point to know where to create a bullet.

so lets say i know the length of the hand and the length of the gun then i have the angle of the hand and with sin/cos i can get x/y but the gun is not in the same line with the hand its a little bit above so what i get is the bullet is created not where the gun is it is created with the 0 line of the hand.

here is a image to explain my problem:

1.png


when angle is 0 i can just say create the bullet with some pixels above the zero line but when angle changes how can i calculate exact x/y of the barrel of the gun?

thanx, ilan
 

klaus

Expert
Licensed User
Longtime User
You can precalulate the distance GunRadius of the gun output from the rotating point.
GunRadius = Sqrt(GunX * GunX + GunY * GunY)
GunX and GunY are the coordinates of the gun output with 0 hand angle.

Then you can precalulate the GunAngle between 0 hand angle and the gun output.
GunAngle = ATan(GunY / GunX)

The you can calculate the gun output coordinates for every hand angle.
x = GunRadius * Cos(HandAngle + GunAngle)
y = GunRadius * Sin(HandAngle + GunAngle)


Then you can shoot, but with the hand angle.

Sketch:
upload_2017-7-20_9-27-49.png


If you rotate:

upload_2017-7-20_9-30-47.png
 

Attachments

  • upload_2017-7-20_9-24-52.png
    upload_2017-7-20_9-24-52.png
    10.6 KB · Views: 185
Last edited:

ilan

Expert
Licensed User
Longtime User
thank you very much klaus.

if the HandAngle and the GunAngle are the same do i still need to calculate the GunAngle?
 

ilan

Expert
Licensed User
Longtime User
thank you for your help klaus but i think i was not clear enough.

the 0 point is not the bottom of the hand its in the middle.

1.png


so the angle of the gun is always the same like the hand.

2.png


i tried your snipped but i could not get the result i wanted. :(
 

strat

Active Member
Licensed User
Longtime User
ilan, I think you need two calculations.

1- Bullet spawn point. Klaus pointed it at his first sketch. You can find it after you found hand angle, using by GunAngle and Gunradius values. You'll add handangle and gunangle to find bullet spawn point.
2- Bullet spawn angle. It is same as hand angle.
 

ilan

Expert
Licensed User
Longtime User
thank you @klaus, it works :)

B4X:
Sub calcBulletXY(GunX As Float, GunY As Float, Angle As Float) As Point
    Dim GunRadius,x,y,GunAngle As Float
    GunRadius = Sqrt(GunX * GunX + GunY * GunY)
'  
    GunAngle = ATan(GunY / GunX)
  
    x = GunRadius * Cos(Angle + GunAngle)
    y = GunRadius * Sin(Angle + GunAngle)
 
    Dim p As Point
    p.Initialize(x,y)
    Return p  
End Sub
 
Top