Author Topic: Debris and Gravity  (Read 11003 times)

0 Members and 1 Guest are viewing this topic.

Offline General Battuta

  • Poe's Law In Action
  • 214
  • i wonder when my postcount will exceed my iq
Fantastic! So AI will of course still freak out and fail, but at least it's progress.

 

Offline swashmebuckle

  • 210
  • Das Lied von der Turd
    • The Perfect Band
Wow, get lift and stalling working and you'll be on your way to an arcade-style atmospheric flight model :yes:

 
 

Offline Sushi

  • Art Critic
  • 211
Awesome! I'll take this for a spin when I get home. :)

Don't we need an updated version of the script that looks for ships with the flag and applies gravity to them?

Also, atmospheric flight is only part of the coolness this allows. I think that if we combine gravity with the landing code, we'll be able to get working land vehicles, such as tanks and pod racers. :D

 

Offline m!m

  • 211
You're right! :nod:
Code: [Select]
#Conditional Hooks

$Application: FS2_Open
$On Game Init:
[
function indexOf(t,val)
    for k,v in ipairs(t) do
        if v == val then
return k
end
    end
end

function g_setG(value)
if type(value) ~= "number" then
ba.warning("Invalid type given to g_setG!")
return
end
globalG = value
end

function g_enable()
enabled = true
end

function g_disable()
enabled = false
end

function g_sGr(value)
local ship = mn.Ships[value]
if ship == nil or not ship:isValid() then
ba.warning("Invalid value given to g_sGr")
return
end
groundShip = ship
end

function doGravity(object)
local vec = ba.createVector(0, -1, 0)
vec = vec * globalG
local newVec = object.Physics.Velocity + vec

object.Physics.Velocity = newVec
end

ignoreDebris = {}

globalG = 10

enabled = true

groundShip = nil

]

$On Debris Collision:
[
if hv.Self:getBreedName():lower() == "ship" and hv.Self == groundShip then
hv.Debris.Physics.Velocity = ba.createVector(0,0,0)
table.insert(ignoreDebris, hv.Debris)
end
]

$State: GS_STATE_GAME_PLAY
$On Frame:
[
if globalG and globalG <= 0 or not enabled then
return
end

for i=0,#mn.Debris do
if indexOf(ignoreDebris, mn.Debris[i]) then
return -- Don't apply gravity on things we ignore
end

doGravity(mn.Debris[i])
end

for i=0, #mn.Ships do
if mn.Ships[i].FlagAffectedByGravity then
doGravity(mn.Ships[i])
end
end
]

#End

Warning: This script will only work with a build that includes the posted patch!

EDIT: Fixed bugs...
« Last Edit: October 23, 2010, 02:35:01 am by m!m »

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Function added to trunk in revision 6669
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline chief1983

  • Still lacks a custom title
  • 212
  • ⬇️⬆️⬅️⬅️🅰➡️⬇️
    • Minecraft
    • Skype
    • Steam
    • Twitter
    • Fate of the Galaxy
With an atmospheric skybox, this would be a nice touch with models like this blowing up everywhere.
Fate of the Galaxy - Now Hiring!  Apply within | Diaspora | SCP Home | Collada Importer for PCS2
Karajorma's 'How to report bugs' | Mantis
#freespace | #scp-swc | #diaspora | #SCP | #hard-light on EsperNet

"You may not sell or otherwise commercially exploit the source or things you created based on the source." -- Excerpt from FSO license, for reference

Nuclear1:  Jesus Christ zack you're a little too hamyurger for HLP right now...
iamzack:  i dont have hamynerge i just want ptatoc hips D:
redsniper:  Platonic hips?!
iamzack:  lays

 

Offline swashmebuckle

  • 210
  • Das Lied von der Turd
    • The Perfect Band
ZOMG@everythinginthisthread!

 

Offline Black Wolf

  • Twisted Infinities
  • 212
  • Hey! You! Get off-a my cloud!
    • Visit the TI homepage!
So, question, will this now mean than only ships with "Affected by Gravity" will have their debris fall down? Because for my purposes, I'd much rather have it set up the way it was, i.e. every ship's debris falls down, but only selected ships (until AI gets smarter, just the player) have to eal with gravity while flying.

I'd test it and ee, but I'm not sure whgat to do with that .patch file :nervous:
TWISTED INFINITIES · SECTORGAME· FRONTLINES
Rarely Updated P3D.
Burn the heretic who killed F2S! Burn him, burn him!!- GalEmp

 

Offline Angelus

  • 210
  • The Angriest Angel
So, question, will this now mean than only ships with "Affected by Gravity" will have their debris fall down? Because for my purposes, I'd much rather have it set up the way it was, i.e. every ship's debris falls down, but only selected ships (until AI gets smarter, just the player) have to eal with gravity while flying.

I'd test it and ee, but I'm not sure whgat to do with that .patch file :nervous:


the patch is for coders

 

Offline Sushi

  • Art Critic
  • 211
A couple of things I've noticed about the script:

1. Last doGravity call should be on mn.Ships, not mn.Debris
2. No adjustment for framerate... I added this by replacing "globalG / 50" in doGravity with "globalG * ba.getFrametime()"
3. Unfortunately, it doesn't work very well on ships unless they have high damp, and then the ship gets unflyable. Even then, terminal velocity is pretty low overall. We're going to have to get creative with how gravity is applied to ships so that we get something reasonable for ships of all damping levels... not sure what.

The debris part works great, though.

EDIT: Where did those italics come from?
« Last Edit: October 23, 2010, 12:51:24 am by Sushi »

 

Offline Herra Tohtori

  • The Academic
  • 211
  • Bad command or file name
I think implementing terminal velocity would be beneficial to this system.

Not only is it mathematically reasonably simple, but it's also going to work as a rudimentary atmospheric friction model.

The maths of it goes like this:

F = -G - kv^n

where

F is the resultant force vector
G is gravitational force (weight) (G=-mg)
k is friction coefficient (determined from values such as shape coefficient and air density)
v is velocity
n is the exponent for velocity, which varies depending on the speed itself, but for a rudimentary system it's more or less safe to approximate that n = 2. Basically this number increases as velocity increases, but that makes the equation needlessly complicated. Usually the friction coefficient is experimentally defined and ranges from zero to 1; for most FS2 ships I suppose it would range between 0.5 and 1.

As for solving the equation, using n = 2:

F = dp/dt = m dv/dt

m dv/dt = -mg - kv^2

dv/dt = -g - (k/m) v^2

1 / (-g - (k/m)v^2)  dv = dt  || Int(..)

v                                     t
∫ 1 / (-g - (k/m)v^2)  dv  = ∫ dt
v0                                    t0
...


v(t) = - (√g√m / √k) * tan ( [√g√k) / √m] t - arctan [(√k / √g√m) v0] )


And there you go; of course, this is a simplified equation where movement is limited to one axis. If you allow movement in all three dimensions, then you have to formulate the air friction so that it's always vectorized against the velocity vector, while gravitational force always pulls "down" (in homogenous gravity field approximation, that's the same direction everywhere in the game world).

If you can hook accelerations into the engine directly instead of velocities, that would make things easier still, since solving the diff eq wouldn't be even necessary, you could just plug the forces divided by mass into the engine and it would apply the accelerations to objects.
There are three things that last forever: Abort, Retry, Fail - and the greatest of these is Fail.

 

Offline chief1983

  • Still lacks a custom title
  • 212
  • ⬇️⬆️⬅️⬅️🅰➡️⬇️
    • Minecraft
    • Skype
    • Steam
    • Twitter
    • Fate of the Galaxy
I read 'terminal velocity' and thought of this:

Fate of the Galaxy - Now Hiring!  Apply within | Diaspora | SCP Home | Collada Importer for PCS2
Karajorma's 'How to report bugs' | Mantis
#freespace | #scp-swc | #diaspora | #SCP | #hard-light on EsperNet

"You may not sell or otherwise commercially exploit the source or things you created based on the source." -- Excerpt from FSO license, for reference

Nuclear1:  Jesus Christ zack you're a little too hamyurger for HLP right now...
iamzack:  i dont have hamynerge i just want ptatoc hips D:
redsniper:  Platonic hips?!
iamzack:  lays

 

Offline Sushi

  • Art Critic
  • 211
Well, yes, a lot of problems would be solved if Freespace used a clear-cut "sum the forces on the body" model like you learn in any first-year physics class. Basic kinematics. For better or for worse, they did something more complicated with damping and ramped desired velocities that makes it a lot harder to just add arbitrary forces to the model. That's what makes this such an annoying problem. :) If they'd done it the simple way Herra just described, adding gravity would be trivial. Unfortunately, they didn't, so it's not. :(


 

Offline Herra Tohtori

  • The Academic
  • 211
  • Bad command or file name
Well, yes, a lot of problems would be solved if Freespace used a clear-cut "sum the forces on the body" model like you learn in any first-year physics class. Basic kinematics. For better or for worse, they did something more complicated with damping and ramped desired velocities that makes it a lot harder to just add arbitrary forces to the model. That's what makes this such an annoying problem. :) If they'd done it the simple way Herra just described, adding gravity would be trivial. Unfortunately, they didn't, so it's not. :(

Blast and damnation. Good to know, then...
There are three things that last forever: Abort, Retry, Fail - and the greatest of these is Fail.

 

Offline Sushi

  • Art Critic
  • 211
For relatively high damping values ($Damp > 2.0 or so) this script actually works pretty well.

I think if we can come up with the right equation, we can adjust the force based on the ship's damping value and have a fairly reasonable approximation that gives about the same effective amount of gravity for different values of damp.

By the way, gravity + landings code didn't lead to land vehicle awesomeness as easily as I had hoped. :( Oh well, back to the drawing board!

 

Offline m!m

  • 211
For relatively high damping values ($Damp > 2.0 or so) this script actually works pretty well.

I think if we can come up with the right equation, we can adjust the force based on the ship's damping value and have a fairly reasonable approximation that gives about the same effective amount of gravity for different values of damp.
This might work very well.

By the way, gravity + landings code didn't lead to land vehicle awesomeness as easily as I had hoped. :( Oh well, back to the drawing board!
That's sad :(

So, question, will this now mean than only ships with "Affected by Gravity" will have their debris fall down? Because for my purposes, I'd much rather have it set up the way it was, i.e. every ship's debris falls down, but only selected ships (until AI gets smarter, just the player) have to eal with gravity while flying.
Gravity is applied to all debris in the mission and to the ships that carry the "affected-by-gravity" flag.

Well, yes, a lot of problems would be solved if Freespace used a clear-cut "sum the forces on the body" model like you learn in any first-year physics class. Basic kinematics. For better or for worse, they did something more complicated with damping and ramped desired velocities that makes it a lot harder to just add arbitrary forces to the model. That's what makes this such an annoying problem. :) If they'd done it the simple way Herra just described, adding gravity would be trivial. Unfortunately, they didn't, so it's not. :(
And it's very difficult to implement I suppose? :(

There are too much sad smilies in this post...

 

Offline Wanderer

  • Wiki Warrior
  • 211
  • Mostly harmless
Hmm... There should be functions/tools in fs open lua for checking bits inside flags but i suppose a direct function is ok as well.

As for how to deal with ground... Setting all terrain pieces (& buildings, scenery etc stuff) into same collision group (see scripting html) to drastically reduce the number of collision checks needed would be advisable. Also using directed ray collision with the terrain pieces allows you to create a crude 'radar altimeter'. Same idea could work with vehicles.
Do not meddle in the affairs of coders for they are soggy and hard to light

 
May that script affect disabled ships/object as well?
I thought about a sort of flying city where when you destroy the underneath engine that maintain it to the sky... after destroying those you'll have this object falling to the "ground"
$Formula: ( every-time
   ( has-time-elapsed "0" )
   ( Do-Nothing
   )
   ( send-message
      "#Dalek"
      "High"
      "Pro-crasti-nate"
   )
   )
)
+Name: Procratination
+Repeat Count: 99999999999
+Interval: 1

 

Offline m!m

  • 211
This could be done by setting the "affected-by-gravity" flag when the engine is destroyed. I don't know if there is a SEXP capable of doing this but it can be done without changes to the script. :nod: