aspose file tools
The moose likes Game Development and the fly likes Leading Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Game Development
Reply Bookmark "Leading" Watch "Leading" New topic
Author

Leading

Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
I'll do my part to the new forum by posting the question I'm currently working on:

{velocities are "per loop" of the game}

Consider a target (T) with a position (xT,yT), and a velocity (deltaXT,deltaYT). Now consider a rotating gun turret (G) with a position (xG,yG), and an orientation theta, in degrees. Said gun turret fires a bullet (B) originating from (xG,yG) (or very close to it), with the velocity in radians (s,theta) changed into rectangular, where 's' is a given magnitude, and theta is the above-mentioned orientation of the GunShip. All coords can be changed to radians and back to rectangular with great ease.

given (x1,y1), (deltaX1,deltaY1), (x2,y2), and s, solve for theta such that the bullet will hit the target.

Note that finding (deltaXB,deltaYB) is not an issue, for once theta is found, it is simply (s,theta) in rectangular.


The game I've been working on has involved endless math and equation-making, and if this forum can serve as a place to bounce ideas around with people way smarter than me, it may be a great addition to the ranch.

-Joe

[Clarification]
[Emboldened main question, so as to draw attention to its significance. Kna'mean?]
[ July 31, 2004: Message edited by: Joseph George ]

I've heard it takes forever to grow a woman from the ground
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The projectile and target both need to travel different distances at different speeds, but it takes them each the same amount of time to cover the distance. Since speed is distance over time, and time is distance over speed, we set the times equal and get this:

Distance Turret / Speed Turret = Distance Target / Speed Target

Distance Turret / Distance Target = Speed Turret / Speed Target

Since we know the speeds, we know this:

The initial distance between the Turret and Target, and the ratio of the distances Turret-Crash and Target-Crash. We also know the measure of angle Turret-Target-Crash. Let's pause to rephrase this without unnecessary infO:

We have a triangle ABC, we know the length of AB, the measure of B, and the value of AC/BC.

The law of sines tells us that AC/sin(b)=BC/sin(a). Thus sin(a)=sin(b)*BC/AC.

We know sin(b) since B is the angle formed by drawing a line from the turret to the target and then in the direction the target is moving. We know BC/AC since that's the ratio of the distances Target-Crash/Turret-Crash. It's just a matter of calculating A, the angle to fire at (where 0 means firing straight at the target, not necessarily due east). I've glossed over the conversion between absolute angles and angles in the triangle formed by the significant points, but here's some code that does the full calculation:



(Edited code. Behavior is identical but removed the laziness where playerToCrashOverTurretToCrash was misnamed)
[ August 01, 2004: Message edited by: David Weitzman ]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Wow. It would be tough to imagine a more helpful and thourough answer. I was able to implement your triangle comparatively quickly. I am very grateful. Now, it is 4:30 AM here, and time for the ol' Joe to go to bed. Thanks.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Leading
 
Similar Threads
how do i rotate a point from a center point??
Cannon fire
Pro Jakarta Velocity: From Professional to Expert by Rob Harrop
Wednesday Evening Physics
The rising(Bollywood flick) - my take