Well, I just wanna simulate the effect of gravity.
what i’m actually doing is put a ball inside a cube and then rotate the cube. I wanna gravity effect on the ball.
i know the idea of continuing minusing a particular value from the ball’s y-value. The problem is after rotation, the ball’s y-axis is no longer the vertical direction.

How to solve this?

Any help is appreciated.

The ball has a point, called the center of gravity, which for a uniform speherical ball coincides with the center of the ball. You apply gravity to the center of gravity, and simply draw the ball around that point.

Depends on how complicated you want to make it, but your first problem should be solved by pushing your modelview matrix before rotation


//rotate cube and draw it

//draw sphere which has y up and down

That should solve your gravity in y axis problem.

Where is the gravity center described?
I could not find it in the documentations.


You must implement gravity and all other physics yourself! That code is just to keep the y axis for the sphere independent of your rotating cube.

There is a tutorial on gravity at but this is basically how it goes.

  1. Make an acceleration vector for gravity such as (0.0f,-9.8f,0,0f).

  2. Add that to a velocity vector for the ball ((0.0f,0.0f,0.0f) if the ball starts still).

3.Then add the result to the ball’s position.

  1. goto step 2. till ball hits ground(or whatever)
    Thats the simple part. Now to keep it in the cube you need some collision code. Go to for some help with that.

Here is something to think about:

Each part of your world has its own XYZ.

You have your worlds XYZ and you have your objects XYZ.

Each object is relative to space in which it is being acted on.

Let’s use your box as an example:
It has six sides, top, bottom, right, left, front, back.

If we rotate the box a full 180 say on the Y axis. Then the Back is rotated to the Front.

Now the worlds XYZ has not changed but the boxes has. It’s positive X is not facing backwards, in respect to the worlds which now has the negitive X axis of the box.

Also if we have a ball inside the box, its X axis will be also rotated also with the box.

In respect to the box you only need to look at the location of the ball in the box. Since both the ball and box current share the same XYZ space.

As respect for gravity, is effect will very based on world location vs. box rotation.
You use this to find force in which to act on the ball

Now well only need to find the angle in which the box is in refernce to the world gravity.

Let’s say at angle 0 the ball is at rest, both the box and world are parallel.
At > 0, the box is tilted to the left and force is starting to move the ball in the +y, and say at < 0 the box is tilted to the other side and ball moves right.

I hope this gives you more of the idea on how to handle gravity.

Well, I want it this way, actually:
the ball’s coordinates rotate with the rotation of the cube’s rotation.
in the meanwhile, in the world coordinates the ball is supposed to have gravity effect as well.

Here is the method I would choose:

do as the bloke above says transform the view to be where ever you want to look at then draw the sphere at (x,y,z) where y is given by:

y= initial speed * time + 0.5 * gravity *time * time

probably if you want it simple make time 0.2ish (representing 0.2 seconds per frame)
A have a counterr toc ount frames so:
each frame

y= u * i * 0.2 + 5 * 0.04 * i*i

Hope this helps,


I thought of the method of using time,
but is there any method to obstain the time passed?
or, is it just unnecessary to use real time?

You can just use seconds past, since the start of motion.
but could be anything like frames processed, but that would make it run faster on some machine and slow on other…

Only buy using time will you get a real effect.

time(&ltime); // Get time
newtime = localtime(&ltime); // Convert to local time

start_time = newtime->tm_sec // pointer to seconds

Then just count seconds or minutes, you also can get raw time also.

Originally posted by Unicron:
I thought of the method of using time,
but is there any method to obstain the time passed?
or, is it just unnecessary to use real time?