Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Jan 2011
Offline Last Active May 27 2016 03:33 PM

Posts I've Made

In Topic: Is this loop any good?

29 November 2015 - 09:40 AM

For me this looks like a main loop doing rendering (you call each iteration for frames) and logic. 
The loop you made is not deterministic, which means how long your 60 frames takes, depends on how long your logic takes to execute, and will differ from system or amount of work.
But that is without knowing how you have setup your rendering API, in other words I am not sure your frames is bound to the screen refresh rate. If VSYNC is enabled(should be to prevent screen tearing or jiggered pixel movement), your loop could be rewritten to something like: 

stopwatch frameTime;
setSwapBufferInterval(1); // Swap buffer by an interval of one frame. 
  swapBuffer(); // Will block until it is time to swapBuffer based on screen refresh rate.
  std::cout << "FPS: " << 1.0 / frameTime.seconds << std::endl; // You should probably throttle FPS printing, by for example taking average FPS every sec.

Beware that there exist screens with 120 hz (and more than that too), which may not be suitable for your physics engine or game logic (too low precision per timestep etc.).
Also having a dedicated rendering thread is what is recommended today. There are unfortunately no standard way of doing it, but there exist per platform solutions. On OS X and iOS you have something called CADisplayLink (CVDisplayLink on OS X). On android you have something called Choreographer. In browser you have requestAnimationFrame. On Windows you make your own thread and use https://msdn.microsoft.com/en-us/library/windows/hardware/ff547265(v=vs.85).aspx from GDI+ (Only works on Vista and upwards).

So now you need to sync rendering and logic, since rendering happens in its own thread, and event handling and other things on the main thread. This can be done by simple communication (in hardware) between your main thread and rendering thread. For example by using semaphores.
Also all the per platform methods above gives you the current frame duration, which is very accurate and can be used later in your time independent code to do proper physic integration or movement. 

I guess this may seem a bit daunting to you, however I have seen a lot of weird solutions to this problem, and in my experience the best way to make a main loop is by reading the docs for your target platform.

In Topic: Physic of ball for review

05 January 2014 - 08:27 AM

First of all your code is not cache friendly at all. You branch your code to much, for example why check if the force vector is empty?
Another thing, you have not made a common solution for physic update. The same update function should work for all types of shapes not only spheres.


The update function should only be a physic integration. As you may recall: a = F / m, v += a * t, s += v * t + 0.5 * a * t * t for linear physic, and angularAcc = torq * I-1, rotation += angularAcc * t, orientation += rotation * t for angular physic.

I see you are doing this, however you should separate your integration code from the contact resolving, so that you can have common solution for all bodies. So what you need to do is to clean up your code, and separate it, and have in mind that your solution should not be specific for each shape, but common. 

Also to paste code from visual studio, turn on "space for tabs" in the editor settings, and then convert your code file before you copy it.

In Topic: Separate matrices for every glDrawCalls

27 December 2013 - 04:07 PM


For your getWorldCoord() functions, I think the case where parent is true should be different, if you want to support nested parenting.

m_position is basically the offset from either origin or parent (whichever is applicable), but your code won't work correctly if the parent also has a parent.

"Box on top of table" would work, but "Book on top of box on top of table" wouldn't.


I think parent->m_position should instead be parent->getWorldCoord().

A similar fix would be needed for your second example.


Thanks for pointing that out, i have edited the post.

In Topic: Separate matrices for every glDrawCalls

27 December 2013 - 10:00 AM

I can't really see your problem. You can create a function that returns the world client position, and use it when you want.
Also, i would have made a struct/class for points/vectors, so that you could easily handle coordinates. 

// Example of how a Vector3 struct may look like
struct Vector3
    int x, y, z;
    Vector3(int x, int y, int z) : x(x), y(y), z(z) {}

    void set(int x, int y, int z)
        x = x;
        y = y;
        z = z;

    Vector3 operator+(const Vector3 &a) const
        return Vector3(x + a.x, y + a.y, z + a.z);

    /* ... add some other neat functions ... */

// Example #1
Vector3 Object::getWorldCoord()
    Vector3 result;
        result = parent->getWorldCoord() + m_position; // position is a Vector3, and is a member of the Object class.
        result = m_position;
    return result;

// Example #2 that works out of the box for you
void Object::getWorldCoord(int &outX, int &outY, int &outZ) 
        int parentX, parentY, parentZ;
        parent->getWorldCoord(parentX, parentY, parentZ);
        outX = parentX + x; 
        outY = parentY + y; 
        outZ = parentZ + z;
        outX = x;
        outY = y;
        outZ = z;

In Topic: How to create a physics engine

14 October 2013 - 02:37 AM

Not at all, Bullet's GPU solver is not yet in release.

Did i mention Bullets? Even if i am wrong with Havok, since it has limited GPU support, however are going to have full on next-gen consoles.

Hmm, poorly expressed by me. What i mean is that AAA physic engines are moving towards GPU acceleration, which is state of the art. With the next-gen consoles, we would see that CPU based physics engine are being slowly thrown away, in favor of GPU based. Why create a CPU based physic engine, when we are missing a good and open-source GPU based physic engine, that are platform independent. Not that he is going to create a open-source engine, but you see what i mean. The question is what's the goal. Do you want to create an useful engine and push the market, or are you going to create an engine just for education(where parallel programming is a good start though).