Example #1
0
        public void mainLoop(float fps)
        {
            _curFPS = fps;
            _timer += 1 / fps;

            Horde3D.setOption(Horde3D.EngineOptions.DebugViewMode, _debugViewMode ? 1.0f : 0.0f);

            if (!_freeze)
            {
                _animTime += 1.0f / _curFPS;

                // Do animation blending
                Horde3D.setModelAnimParams(_knight, 0, _animTime * 24.0f, _weight);
                Horde3D.setModelAnimParams(_knight, 1, _animTime * 24.0f, 1.0f - _weight);

                // Animate particle system
                Horde3D.advanceEmitterTime(_particleSys, 1.0f / _curFPS);
            }

            // Set camera parameters
            Horde3D.setNodeTransform(Horde3D.PrimeTimeCam, _x, _y, _z, _rx, _ry, 0, 1, 1, 1);


            if (_showFPS)
            {
                // Avoid updating FPS text every frame to make it readable
                if (_timer > 0.3f)
                {
                    _fpsText = string.Format("FPS: {0:F2}", fps);
                    _timer   = 0;
                }

                // Show text
                if (_fpsText != null)
                {
                    Horde3DUtils.showText(_fpsText, 0, 0.95f, 0.03f, 0, _fontMatRes);
                }

                string text = string.Format("Weight: {0:F2}", _weight);
                Horde3DUtils.showText(text, 0, 0.91f, 0.03f, 0, _fontMatRes);
            }

            // Show logo
            Horde3D.showOverlay(0.75f, 0, 0, 0, 1, 0, 1, 0,
                                1, 0.2f, 1, 1, 0.75f, 0.2f, 0, 1,
                                7, _logoMatRes);


            // Render scene
            Horde3D.render();

            // Write all messages to log file
            Horde3DUtils.dumpMessages();
        }
        public void update(float fps)
        {
            // Parameters for three repulsion zones
            float d1 = 0.25f, d2 = 2.0f, d3 = 4.5f;
            float f1 = 3.0f, f2 = 1.0f, f3 = 0.1f;

            for (int i = 0; i < _particles.Count; ++i)
            {
                Particle p = _particles[i];

                // Reset force
                p.fx = 0; p.fz = 0;

                // Calculate distance to destination
                float dist = (float)Math.Sqrt((p.dx - p.px) * (p.dx - p.px) + (p.dz - p.pz) * (p.dz - p.pz));

                // If destination not reached walk to destination
                if (dist > 3.0f)
                {
                    // Calculate normalized attraction force to destination
                    float afx = (p.dx - p.px) / dist;
                    float afz = (p.dz - p.pz) / dist;

                    p.fx += afx * 0.035f; p.fz += afz * 0.035f;

                    // Repulsion forces from other particles
                    for (int j = 0; j < _particles.Count; ++j)
                    {
                        if (j == i)
                        {
                            continue;
                        }

                        Particle p2 = _particles[j];

                        float dist2    = (float)Math.Sqrt((p.px - p2.px) * (p.px - p2.px) + (p.pz - p2.pz) * (p.pz - p2.pz));
                        float strength = 0;

                        float rfx = (p.px - p2.px) / dist2;
                        float rfz = (p.pz - p2.pz) / dist2;

                        // Use three zones with different repulsion strengths
                        if (dist2 <= d3 && dist2 > d2)
                        {
                            float m = (f3 - 0) / (d2 - d3);
                            float t = 0 - m * d3;
                            strength = m * dist2 + t;
                        }
                        else if (dist2 <= d2 && dist2 > d1)
                        {
                            float m = (f2 - f3) / (d1 - d2);
                            float t = f3 - m * d2;
                            strength = m * dist2 + t;
                        }
                        else if (dist2 <= d1)
                        {
                            float m = (f1 - f2) / (0 - d1);
                            float t = f2 - m * d1;
                            strength = m * dist2 + t;
                        }

                        p.fx += rfx * strength; p.fz += rfz * strength;
                    }
                }
                else
                {
                    // Choose new destination
                    chooseDestination(ref p);
                }

                // Make movement frame rate independence
                p.fx *= (30 / fps);
                p.fz *= (30 / fps);
                float vel = (float)Math.Sqrt(p.fx * p.fx + p.fz * p.fz);

                // Set new position
                p.px += p.fx; p.pz += p.fz;

                // Caluclate orientation
                p.ox = (p.ox + p.fx) / 2;
                p.oz = (p.oz + p.fz) / 2;

                // Get rotation from orientation
                float ry = 0;
                if (p.oz != 0)
                {
                    ry = (float)Math.Atan2(p.ox, p.oz);
                }
                ry *= 180 / 3.1415f;            // Convert from radians to degrees

                // Update character scene node position
                Horde3D.setNodeTransform(p.node, p.px, 0.02f, p.pz, 0, ry, 0, 1, 1, 1);

                // Update animation
                p.animTime += vel * 35.0f;
                Horde3D.setModelAnimParams(p.node, 0, p.animTime, 1.0f);
            }
        }