Author Topic: WIP: Dog fight in space...sort of...  (Read 10506 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #15 on: July 14, 2019, 03:23:12 pm »
I've installed the grid I mentioned above. As units fly farther from, or nearer to, each other the grid will resize by factors of ten as the necessary display scale changes. It was a head scratcher, but I managed to do it in 18 lines of code, in SUB SensorScreen.

I'm pushing all changes into the original posting. I assume this is good etiquette, but correct me if I'm wrong.

I do that all the time. I think it is far better to have one posted program, edited as needed, rather than 15 ongoing posts in the same thread. I usually include a update on what I changed in the text part of the post.

Of course, doing it my way doesn't mean you're doing it the right way... just the BEST way! ;D

Pete

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #16 on: July 20, 2019, 01:30:55 pm »
In trying to come up with a good way to handle planet collision, I came across the concept of ray tracing. I've never done anything like it before so I thought I'd do a test bed program just to see it in operation. More a proof of concept exercise than a WOW graphics ap. Since a similar algorithm is going in the main project, I thought I'd share my first attempt.

Now if I could just get it to work in the main project. It has not been cooperating there, so hopefully these little test programs can teach me something.

EDIT: improved loop exit handling.

Code: QB64: [Select]
  1. TYPE unitpoint
  2.     X AS SINGLE
  3.     Y AS SINGLE
  4.     Z AS SINGLE
  5.  
  6. A& = _NEWIMAGE(1200, 700, 32)
  7. SS& = _NEWIMAGE(620, 620, 32)
  8. EL& = _NEWIMAGE(300, 300, 32)
  9.  
  10. SCREEN A& '                                                     Initiate main screen
  11. _TITLE "Sphere intersection test bed"
  12. _SCREENMOVE (_DESKTOPWIDTH - 1200) / 2, 1
  13.  
  14.  
  15. DIM SHARED S_C AS unitpoint '                                   sphere center point
  16. DIM SHARED V_S AS unitpoint '                                   vector starting point same as cmbtnt(x).OldXYZ
  17. DIM SHARED V_E AS unitpoint '                                   vector ending point same as cmbtnt(x).AbsXYZ
  18. DIM SHARED ni AS unitpoint '                                    near intersection point
  19. DIM SHARED fi AS unitpoint '                                    far intersection point
  20. DIM SHARED Rd AS INTEGER '                                      radius of sphere
  21.  
  22.  
  23.     COLOR , _RGBA32(50, 50, 50, 255)
  24.     CLS
  25.     V_S.X = 0: V_S.Y = 0: V_S.Z = 0
  26.     INPUT "Sphere center X: ", S_C.X
  27.     INPUT "Sphere center Y: ", S_C.Y
  28.     INPUT "Sphere center Z: ", S_C.Z
  29.     INPUT "Sphere radius  : ", Rd
  30.     INPUT "line endpoint x: ", V_E.X
  31.     INPUT "line endpoint y: ", V_E.Y
  32.     INPUT "line endpoint z: ", V_E.Z
  33.  
  34.  
  35.     dx = V_E.X - V_S.X: dy = V_E.Y - V_S.Y: dz = V_E.Z - V_S.Z
  36.     A = dx ^ 2 + dy ^ 2 + dz ^ 2
  37.     b = 2 * dx * (V_S.X - S_C.X) + 2 * dy * (V_S.Y - S_C.Y) + 2 * dz * (V_S.Z - S_C.Z)
  38.     c = S_C.X ^ 2 + S_C.Y ^ 2 + S_C.Z ^ 2 + V_S.X ^ 2 + V_S.Y ^ 2 + V_S.Z ^ 2 + -2 * (S_C.X * V_S.X + S_C.Y * V_S.Y + S_C.Z * V_S.Z) - Rd ^ 2
  39.     disabc = b ^ 2 - 4 * A * c ' if disabc <0 then no intersection =0 tangent >0 intersects two points
  40.     PRINT
  41.     IF disabc >= 0 THEN
  42.         t = (-b - (b ^ 2 - 4 * A * c) ^ .5) / (2 * A)
  43.         tf = (-b + (b ^ 2 - 4 * A * c) ^ .5) / (2 * A)
  44.         ni.X = V_S.X + t * dx: ni.Y = V_S.Y + t * dy: ni.Z = V_S.Z + t * dz
  45.         fi.X = V_S.X + tf * dx: fi.Y = V_S.Y + tf * dy: fi.Z = V_S.Z + tf * dz
  46.     END IF
  47.     short_impact
  48.     MakeDisplay
  49.     IF disabc < 0 THEN
  50.         PRINT "No intersection"
  51.         impact = 0
  52.     ELSEIF disabc = 0 THEN
  53.         PRINT "Tangent to"
  54.         PRINT "near at: "; ni.X; ", "; ni.Y; ", "; ni.Z
  55.         PRINT si
  56.     ELSE
  57.         PRINT "Two intersections"
  58.         PRINT "near at: "; ni.X; ", "; ni.Y; ", "; ni.Z
  59.         PRINT "far at:  "; fi.X; ", "; fi.Y; ", "; fi.Z
  60.         PRINT si
  61.     END IF
  62.     PRINT: PRINT "any key to continue, ESC to quit"
  63.     SLEEP
  64.     IF _KEYDOWN(13) THEN a$ = INKEY$  
  65.  
  66. SUB short_impact
  67.     IF Pythagorus(ni, V_S) > Pythagorus(V_E, V_S) THEN
  68.         si = "short"
  69.         impact = 0
  70.     END IF
  71.     IF Pythagorus(ni, V_S) <= Pythagorus(V_E, V_S) THEN
  72.         si = "impact"
  73.         impact = -1
  74.     END IF
  75.  
  76.  
  77. FUNCTION Pythagorus (var1 AS unitpoint, var2 AS unitpoint)
  78.  
  79.     'Use to find distance between two 3D points
  80.     'Also calculate speed of updated vectors
  81.  
  82.     DIM diff AS unitpoint
  83.     diff.X = ABS(var1.X - var2.X)
  84.     diff.Y = ABS(var1.Y - var2.Y)
  85.     diff.Z = ABS(var1.Z - var2.Z)
  86.     horizontal = _HYPOT(diff.X, diff.Y)
  87.     Pythagorus = _HYPOT(horizontal, diff.Z)
  88.  
  89. END FUNCTION 'Pythagorus
  90.  
  91. SUB MakeDisplay
  92.  
  93.     'overview plane display
  94.     _DEST SS&
  95.     CLS
  96.     COLOR _RGBA32(0, 0, 0, 255)
  97.     VIEW (1, 1)-(618, 618), _RGBA32(0, 0, 0, 255), _RGBA(0, 168, 168, 255) '                  set graphics port full image SS& w/box
  98.     WINDOW (-100, 100)-(100, -100) '                        set relative cartesian coords
  99.  
  100.     LINE (0, 50)-(0, 25), _RGBA32(0, 168, 168, 255) '                             draw active unit reference point xhair
  101.     LINE (0, -25)-(0, -50), _RGBA32(0, 168, 168, 255)
  102.     LINE (-50, 0)-(-25, 0), _RGBA32(0, 168, 168, 255)
  103.     LINE (25, 0)-(50, 0), _RGBA32(0, 168, 168, 255)
  104.  
  105.     CIRCLE (S_C.X, S_C.Y), Rd, _RGBA32(127, 127, 127, 255)
  106.     LINE (0, 0)-(V_E.X, V_E.Y), _RGBA32(127, 127, 127, 255)
  107.  
  108.     IPoint ni
  109.     IPoint fi
  110.  
  111.     'elevation plane display
  112.     _DEST EL&
  113.     CLS
  114.     COLOR _RGBA32(0, 0, 0, 255)
  115.     VIEW (1, 1)-(298, 298), _RGBA32(0, 0, 0, 255), _RGBA(0, 168, 168, 255) '                  set graphics port full image SS& w/box
  116.     WINDOW (-100, 100)-(100, -100) '                        set relative cartesian coords
  117.     LINE (-100, 0)-(100, 0), _RGBA32(0, 168, 168, 255)
  118.     CIRCLE (S_C.X, S_C.Z), Rd, _RGBA32(127, 127, 127, 255)
  119.     LINE (0, 0)-(V_E.X, V_E.Z), _RGBA32(127, 127, 127, 255)
  120.  
  121.     _DEST A&
  122.     SCREEN A&
  123.     _PUTIMAGE (560, 18), SS&, A& '                              update sensor screen to mainscreen
  124.     _PUTIMAGE (25, 350), EL&, A&
  125.  
  126.  
  127. SUB IPoint (var AS unitpoint)
  128.     IF impact THEN
  129.         SELECT CASE S_C.Z
  130.             CASE IS < var.Z
  131.                 c& = _RGBA32(11, 127, 238, 255)
  132.             CASE IS = var.Z
  133.                 c& = _RGBA32(222, 222, 222, 255)
  134.             CASE IS > var.Z
  135.                 c& = _RGBA32(244, 6, 67, 255)
  136.         END SELECT
  137.         LINE (var.X - 0.5, var.Y + 0.5)-(var.X + 0.5, var.Y - 0.5), c&, BF
  138.     END IF
  139.  
  140.  
  141.  
  142. 'Ray - Sphere Intersection
  143. 'A sphere is given by its center (cx, cy, cz), its radius R, and its color (SR, SG, SB).
  144. 'A line segment (ray) is given by its endpoints: P0 = (x0, y0, z0) and P1 = (x1, y1, z1).
  145. 'To find visible spheres, set P0 = viewer’s coordinates, VP = (VPx, VPy, VPz) and let P1 run through
  146. 'all the points (x1, y1, 0) where (x1, y1) is a pixel in the display area.
  147.  
  148. 'except that OldMoses is just checking for one ray to impact the sphere's limits.
  149.  
  150. 'Set dx = x1 - x0
  151. 'dy = y1 - y0
  152. 'dz = z1 - z0
  153. 'a = dx*dx + dy*dy + dz*dz;
  154. 'b = 2*dx*(x0-cx) + 2*dy*(y0-cy) + 2*dz*(z0-cz);
  155. 'c = cx*cx + cy*cy + cz*cz + x0*x0 + y0*y0 + z0*z0 +
  156. '-2*(cx*x0 + cy*y0 + cz*z0) - R*R;
  157. 'discriminant(a, b, c) = b^2 - 4*a*c
  158.  
  159. 'if discriminant (a, b, c)
  160. '< 0 no intersection
  161. '= 0 ray is tangent to sphere
  162. '> 0 ray intersects sphere in two points
  163. 'The intersection nearest P0 is given by:
  164. 't = (-b - sqrt(b^2 - 4*a*c))/(2*a)
  165. 'To find the coordinates of the intersection point:
  166. 'x = x0 + t*dx y = y0 + t*dy z = z0 + t*dz
  167.  
« Last Edit: July 21, 2019, 06:04:24 am by OldMoses »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #17 on: July 21, 2019, 06:56:59 pm »
With temperatures being pretty stiff today, the most strenuous thing I'm doing is typing.

In attempting to craft a stable ray tracing algorithm (found in SUB ColCheck) to compute spherical intersection given vast distances represented by kilometers stored in _INTEGER64 variables, and vector speeds that could potentially exceed thousands of kilometers per second over 1000 second turns, it became apparent after several days of working with the equations that _FLOAT values were the only option. Many of the calculations involve adding and multiplying together squares of millions and billions in all three axes, for which even _INTEGER64's range is inadequate. Until I converted to _FLOATs the algorithm simply refused to work, it wasn't until I monitored certain key variables for value that I realized this, I found them to be pegged to the minimum floor of _INTEGER64 (i.e. -9.223372 x 10^18) for relatively short range tests. All just to see if one smacks a ball...

The other issue I encountered after fixing this was that it seemed to matter if the ship hit the planet or if the planet hit the ship. That is, if I flew into it, or if I parked in its path and allowed it to plow into me. The bandaid for that was to test if the ship ended its turn within the sphere, but I suspect that would not yield an impact point without going through the calculations. Planet moves occur after ship moves so this is not surprising.

At any rate, it's working better than it was, and it's been an interesting education. I also added a random ship placement routine to mix up the scenarios a bit. It was getting boring starting out at the same point near the sun. Now there's no telling where you'll end up.

Now to let the smoke clear from all that demolition...imagine the joules I've unleashed. ;)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #18 on: November 22, 2019, 07:17:56 am »
What a difference a long hiatus makes.

A persistent bug that involved the program bogging down when one tries to zoom in to closer detail was deviling me to the point that I threw up my hands and went to another project. I tried reviewing the math procedures, optimizing and changing parameters or variable types. Everything. I gave up and shelved the project indefinitely.

Something made me start tinkering with it again yesterday. After fiddling around with various bits of flotsam and nybbles of jetsam, there came the "DOH!" moment.

It was the circle fill routine, that was being called from SUB SysMap to fill the circles of stars and planetary bodies. Even those that weren't being rendered to the viewscreen, as they were too far away to be displayed, and yet the processing time was being used to do so. The fix was a simple IF...THEN block to encompass the planetary draw routine and reject distant objects.

It wasn't a big deal when zoomed out, as the celestial bodies were small and quickly filled, but upon zooming in, the program was filling enlarged circles of planets, gas giants, moons and sun that were no where near the display area. It only took four months vacation for it to occur to me, and that is why I would never dare try to program for a living. ;)

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #19 on: November 22, 2019, 12:05:00 pm »
Eureka Moments - Priceless

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #20 on: December 06, 2019, 07:48:49 am »
Added a few new toys to this. Two of the simpler ones are an orbit track toggle "O", in the spirit of reduced clutter, and the ability to place the active unit anywhere on the sensor screen via a right click of the mouse.

Also implemented, is a sensor occlusion routine, where the active unit will not be able to "see" units obscured by celestial bodies. Using the same ray tracing routine used for collision checks, it was found to fail to work around the outer planets, while working fine near the inner ones. It had to be the large coordinate numbers, and so implementing a relative coordinate system with the active ship as origin 0,0,0 fixed the problem just as expected.

It also improved the left mouse button selection of the active ship in the sensor screen, which was also found to have more problems in the outer reaches than in the inner. Still chasing a bug with that one, where any unit closely aligned on x or y axis can't be chosen.

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #21 on: December 06, 2019, 05:32:13 pm »
I'm in awe! If you are a 10 on the QB64 programmers scale I might be a 3 if I am lucky.
My name is Michael, but you can call me Mike :)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #22 on: December 06, 2019, 06:33:43 pm »
I'm in awe! If you are a 10 on the QB64 programmers scale I might be a 3 if I am lucky.

Thank you, but if you look over the code for a while you'll see the marks of a number of others here who've made my education possible in the last year or so, particularly in graphics/image manipulation, of which I've only scratched the surface of what's possible. It's kind of like sitting on the shoulders of giants, but if it gets the job done...

As for me, I generally approach a project with the attitude that I'm too stupid to realize I can't do it, and if I bumble around with it long enough, it eventually works. Sorta like 1000 monkeys banging on typewriters. I also tend to program like Arnie Cunningham fixing "Christine", I put brand new windshield wipers on busted windshields.

I started this over 20 years ago with the idea to take some of the tedium out of an old table top RPG game and a half baked Trig approach to the problem, but the old QBasic and QB45 just couldn't handle the range of computations that I needed. Now QB64 has me covered on that front. I never dreamed I'd get it to this point.

The fascinating thing about programming is that the "10" guys can sometimes find new things in the "3" guy's approach.
« Last Edit: December 06, 2019, 06:35:27 pm by OldMoses »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #23 on: December 09, 2019, 12:44:09 pm »
Added some visual bling to, hopefully, make this more user friendly. I'm becoming a little more used to working with images.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #24 on: December 14, 2019, 07:26:08 pm »
Ok, so now I'm calling this version 0.31, this is where I put new hubcaps on the flat tire. I also swatted numerous bugs that various playtesting runs revealed.

Still trying to nail down a control and display layout, but at least I've got it so it's far easier to modify things as I need to with respect to colors, positions, etc. Hopefully, it's becoming moderately more intuitive to use. I'm starting to gain a better understanding of how to work with images. God, I love this language!

Now new ships can be added, old ones can be deleted and those destroyed by planetary collision can be "purged" from the data array. Probably the flashiest addition is a graphics based vector input routine, allowing thrust, azimuth and inclination to be entered via the mouse after clicking the "Vector" button. I've retained the old hotkey "V" text input routine for those who want more precise control of the numbers.

Playing around with button "dimming" so that the user has some idea that something happened, though I'm still retaining hotkeys as they run much faster in certain situations by not having the imaging overhead of mouse clicks. This is particularly true of turn advances.

It's certainly come a long way from the old STARRUTR.BAS
starrutr.jpg
* starrutr.jpg (Filesize: 55.56 KB, Dimensions: 913x513, Views: 378)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #25 on: December 25, 2019, 06:30:35 pm »
For version 0.33 I gave myself the Christmas present of center display that shows ship orientation, thrust status and stars panning to indicate direction of movement. It's a little bit gimpy, speeds up and slows down, doesn't always match exactly to the actual direction, but gives a general idea.

Also added a Z rotation "slider" along the right side to aid the user in viewing the 3D environment, especially useful in determining where other ships fall with respect to ranging when they are out of the Z plane of the active unit. Clicking in and/or dragging along the slider changes the viewing angle. The upper and lower blocks of the slider 'zero' on the Y plane and the center one zeros on the default Z plane.

It is constrained to pivoting around the Y axis. I don't know if the math is technically correct, but it looks relatively convincing. The little vector tails displayed with moving ships need to be altered to reflect the perspective change, but I'll get around to that at some point.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: WIP: Dog fight in space...sort of...
« Reply #26 on: February 16, 2020, 09:26:51 am »
Version 0.34 has been a hoot. {eyeroll here}

It seemed like everytime I fixed something or added something, I would break something else.

I've attached it, in the original post, as a zip file with a new directory structure and executable of the latest source. Source code update is still posted in the code window, but you'll need the directory structure for it to do anything other than error out. It's dependent upon various image and setup files.

Alternate star systems can be loaded, though Sol is still included as a default for practice and/or those not familiar with the game conventions of system creation. I still need to write an input ap for installing new systems. A date input handles ephemeris details, IOW, if you leave a system and come back months or years later the planets will have moved accordingly.

I've been boning up on coordinate transformations and added some improvements in that vein.

Ship placement via right mouse button clicks now places vessels according to the plane of rotation around the X axis. It's now possible to place a vessel in another Z position without resorting to text editing input. I think I've also gotten the coordinate transformation equations corrected. It "looks" right at any rate. Red and blue shift of displayed ships now reflects the relationship to the "plane of view" rather than the absolute Z position. I'm still on the fence as to whether to try adding multi-axis rotations.

On that subject, rotating to negative angles is almost indistinguishable from positive ones and I added a goofy orienteering visual. At negative angles the planet names are displayed inverted. Yes, it is weird. :D

Added a target lock feature with two different target lock ranges to reflect difference between civilian and military vessels. Target locks may be acquired at 1/2 light second or 2 light seconds, respectively. When range exceeds 3 light seconds the lock is lost. The padlock icon in the ship data display acquires the lock if the icon is in red. If it is grey, then target lock is not yet possible.

Thanks to Steve for his MEM tutorials, I've been using the ap to play with it some. I've also implemented $NOPREFIX, bit operations and SUBed out some image stuff to reduce computation times. It will need QB64 version 1.4 to run now. I took out most of the underscores, apologies to anyone who hasn't upgraded yet.

Fixes:
left mousebutton ship selection in the "astrogator" screen is now easier and more reliable.

Vector "tails" corrected for viewing plane transformations.

Present issues:
Ships will sometimes spontaneously be destroyed under extreme outsystem maneuvers. Now there's a headscratcher...