Jump to content

CPU Load of a Raycast


Recommended Posts

I'm thinking of creating a plugin that would be calling several raycasts at once, up to a couple dozen. I'm not sure on the refresh rate or the exact number, but how much of an impact would that have on someone's framerate? Would it be more beneficial to decrease the number of raycasts, or their frequency in order to improve framerate?

Link to comment
Share on other sites

I can't say directly; but under normal circumstances KSP already runs a lot of raycasts every physics frame. They're used for determining if most engines are permitted to create thrust, if solar panels are in sunlight, and the entire wheel collider system is based off a raycast through the bottom of the wheel.

Link to comment
Share on other sites

I asked this question on some Unity forums a while back and got an answer of "it depends". Primarily on:

  • Range of raycast: minimize it for improved performance
  • Complexity of colliders in the scene. Apparently number isn't too important, but loads of mesh colliders in a scene will be slower than loads of boxes. Same as the rest of the physics, really
  • Collision mask of raycast versus colliders: probably not too applicable in KSP, but only raycast against the layers you really need.

One solution is to raycast every nth physics frame, or spread your casts out over several physics steps (5 per frame for 5 frames maybe, then loop back) if you can. Essentialy, does it really need to run constantly?

Link to comment
Share on other sites

I asked this question on some Unity forums a while back and got an answer of "it depends". Primarily on:

  • Range of raycast: minimize it for improved performance
  • Complexity of colliders in the scene. Apparently number isn't too important, but loads of mesh colliders in a scene will be slower than loads of boxes. Same as the rest of the physics, really
  • Collision mask of raycast versus colliders: probably not too applicable in KSP, but only raycast against the layers you really need.

One solution is to raycast every nth physics frame, or spread your casts out over several physics steps (5 per frame for 5 frames maybe, then loop back) if you can. Essentialy, does it really need to run constantly?

For the purposes of this plugin, it is essential that they run constantly, but the range would never need to be more than 500m. You could even get away with 100m. I think a slower refresh would be acceptable though, down to about five per second (per raycast). That's a bare minimum though.

Link to comment
Share on other sites

Whoa, what is this? :P

Well, I'm not sure what the exact physics timestep KSP uses is, the Unity default is 10 ticks per simulated second I think. So refreshing 5 per second is every second tick. I'd test that withrather complex ships with lots of colliders and see what kind of performance penalty it gives.

Link to comment
Share on other sites

No point in keeping it a secret, I suppose. I want to make a terrain radar that is visible while in the cockpit for IVA landing. The idea is that each raycast would show up as a dot. Their size, relative to the size of the center dot that represents actual height above terrain, would increase or decrease. This would give the pilot an idea of the terrain's slope. A field of 25 would get the job done fine.

Link to comment
Share on other sites

that is actually a really neat idea, when tweakables come in, you think it would be possible to have it as a separate option?

think the radar thingy from Half-Life 2 episode 2, in the muscle car

Thanks, but I don't know how it would integrate with tweakables. I was thinking of having a few switches on the GUI to activate/deactivate it, change scan area, etc. Then, if a crapton of raycasts doesn't bog down framerate, there could be an "advanced" version later in the tech tree with more points scanned.

Link to comment
Share on other sites

Shiny! If you're doing this, and if you could find out if KSP uses layer tags differentiating terrain and parts, you could easily raycast just against the terrain layer. This would allow you tons more headroom in performance!

Link to comment
Share on other sites

Shiny! If you're doing this, and if you could find out if KSP uses layer tags differentiating terrain and parts, you could easily raycast just against the terrain layer. This would allow you tons more headroom in performance!

That sounds like what I want to do. I have some learning to do though.

Link to comment
Share on other sites

I know you can height from sea level (vessel.altitude) and from terrain (vessel.heightFromTerrain) to directly below your ship.

Note that vessel.heightFromTerrain returns from the seabed if you are over water on Kerbin. I had to implement a check to use the smallest of those two values when in Kerbin's SOI for my mod.

I don't know how to reference any other place on terrain other then directly below your ship however.

Maybe you can do something with relative lat/longs?

D.

Link to comment
Share on other sites

Raycast onto the terrain is going to be pretty cheap, normally, so you should be able to do a bunch without noticing. Try it out, benchmark, see if it's acceptable. But my bet is that with < 100 per frame you won't be suffering.

In my work window I'm raycasting left and right without suffering any visible degradation in performance on an old iPad.

Link to comment
Share on other sites

Raycast onto the terrain is going to be pretty cheap, normally, so you should be able to do a bunch without noticing. Try it out, benchmark, see if it's acceptable. But my bet is that with < 100 per frame you won't be suffering.

In my work window I'm raycasting left and right without suffering any visible degradation in performance on an old iPad.

Thanks. From what I'm seeing, I think a raycast is the way to go since I can specify an offset from the craft's CoM. Glad to hear it won't be too taxing.

Link to comment
Share on other sites

It would be pretty neat to have a laser ranger part for kOS. Combining more or less of those will give you very flexible configuration and usage options :)

There are more sensors I would love to see, but having laser rangers is the primary wish.

Link to comment
Share on other sites

This sounds like a great tool! When will you alpha/beta/release it?

Flying at night just got a LOT more exciting. Make sure that this is usable not just when landing, too. Some kind of futuristic 3D HUD in the cockpit would also be awesome.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...