physics engine - phase order and other general information

扶醉桌前 提交于 2019-12-02 04:46:00

the order of your phases are not so important. Yes they do mess each other but any dis-function can be corrected by adjusting constants ...

  1. getting started

    you should start with object representation and visualization prior to any simulation code. You need to be able to visualize your stuff so you can see if is all OK or not

  2. object classes

    so create class(es) for all object types you want to support and add few basic interface functions (virtual base class is a good idea for future implementation) like:

    • load(file,ini,stream...),save(file,ini,stream...)
    • draw(screen or render context),update(dt),bool colide(object),etc...

    in time you will see exactly what you need to add ... They will be empty for now (instead the draw which you need to code...). You can also prepare few common physics variables (also as virtual) like position, speed, temperature,...

  3. base class

    it should be the encapsulation of your engine. It hold the lists of all the objects/stuff. Main interface with GUI/App. For more insight of what I mean by now look here Drag&Drop editor in C++ you can use it as start point just add the physics iteration/update to it. Do not forget to implement the object interface into this main class like:

    • load(filename) ... which load all the objects
    • draw(...) ... which draws all the objects ...
  4. simulation

    start with this only if the above is already working. You need to take into account the accuracy and response time you want to achieve. The more fast object/processes are the higher simulation loop frequency you need. For mine simulations I usually use 1-100 ms per iteration. For very fast simulation you can do N iterations per one timer call. That is the meaning of the count and frequency. Where frequency is the timer call speed your iteration loop is called and count (N) is the time step dt division so if N=10 and f=60Hz it means you iterate 10 times every 1/60 seconds so it is the same as N=1 f=600Hz but at least in Windows timer resolution is 1 ms and not very precise ether. So frequencies above 100 Hz are unreliable. if you want to be even more precise you can measure the time by yourself via PerformanceCounter or RDTSC or any precise enough time API you have at disposal.

    you can use D'Alembert principle (simple integration) for the simulation of motions the rest compute with known equations (do not know all of what you want to simulate). For example look here simple mass points gravity simulation in 3D (C++)

    if your code is well written you can parallelize the iteration loop but you have to take in mind that it can create few troubles like double collision reaction,etc

  5. collisions

    there are tons of stuff about this here on SO/SE so just search for it. I think you should look here: simulation of particle collisions to get the idea of multiple collisions handling.

To get the idea here is example of how one of mine simulations looks like

it uses all the stuff I wrote here about and in addition it uses a special class for bonds like springs or joints. This is how it looks like:

struct _bond
    {
    physics_point *pnt0,*pnt1;
    double l0,l1;
    int _beg0,_end0;
    int _beg1,_end1;
    List<int> depend0,depend1;
    int _computed;
    _bond()     {}
    _bond(_bond& a) { *this=a; }
    ~_bond()    {}
    _bond* operator = (const _bond *a) { *this=*a; return this; }
    //_bond* operator = (const _bond &a) { ...copy... return this; }
    };
List<_bond> bnds;

I could cover whole book with the stuff behind this but I am too lazy for that and also this site is not the right place for this so just in a hurry the important stuff is that each bond has it start and end object pointer (pnt0,pnt1) and during computations are filled the dependency lists (depend0,depend1).

  • depend0 for all consequential bonds from pnt0 side
  • depend1 for all consequential bonds from pnt1 side.

Then during each iteration is recursively updated all bonds to match the bond/collision conditions only when they all do then the positions are updated at once for all of them.

  • _computed flag is just to announce that this bond is OK (conditions are met).

The rest is just temp variables to ease up the burden on the recursion heap/stack trashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!