Question

I have 8 squares in a sphere (from a top down view: ooo) and I was wondering how I would o o ooo make these rotate when the sphere moves, but I am not sure how to rotate them round a z axis (I am using a 2d program).

So far if I move up it works fine, or if I move right it works fine but if I move up THEN right then it just changes into a small square.

Sorry if I am not explaining it right but I am only 14.

Project so far: http://scratch.mit.edu/projects/11297983 (I know I am using a kids program)

Thanks for any help!

Was it helpful?

Solution

I don't see any rotation in your program. When I move the sphere with the arrow keys, the squares oscillate wildly, but the “code” behind this looks purely 2d.

To get all of this into 3d, you should start by using 3 coordinates, in such a way that x²+y²+z²=r². This is the condition for a point on the sphere with radius r. It might be easiest to choose r=1 and scale things later on when drawing. Next you can have a look at 3d rotation matrices. The matrix notation might be confusing at first, but expressed in terms of coordinates, it is rather simple. Take e.g. the z rotation:

xnew = xold*  cos(phi)  + yold*sin(phi) + zold*0
ynew = xold*(-sin(phi)) + yold*cos(phi) + zold*0
znew = xold*  0         + yold*0        + zold*1

The above is rather verbose, you could of course drop all these *0 terms completely, omit the *1 factor, and in fact leave z alone altogether. The point here is, matrix multiplication might look scary, but it isn't.

Once you have 3d coordinates, and know how to change them in response to a rotation, you can use two of the three coordinates to draw things, resulting in an orthogonal projection of the scene.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top