0

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!

user1118321
  • 25,567
  • 4
  • 55
  • 86
Lily
  • 1,386
  • 2
  • 13
  • 16
  • 2
    What behavior are you expecting? It looks to me to be working in a consistent manner if those squares are fixed inside a sphere and the motion of the sphere is performed by rolling it around. – andand Jul 08 '13 at 21:22
  • I think I see the problem, but your question is still unclear. What do you mean you're using a 2D program? What's 2D about it? How are these 8 objects represented in the code? What properties does one have? What do you mean by a `z` axis? – Beta Jul 08 '13 at 22:35

1 Answers1

1

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.

MvG
  • 57,380
  • 22
  • 148
  • 276