views:

42

answers:

2

Basically, I'm trying to do 3D projection on a 2D canvas with simulation of depth. As a general rule, bodies that are further away are "shaded" more and smaller than bodies that are closer to the viewer. The only thing missing is having bodies that are further away always drawn behind bodies that are closer.

Sometimes, bodies that are further away are drawn behind closer bodies, but there are always small bodies that get drawn in front of larger ones, meaning that the ones that are further away sometimes appear in front of bodies that are supposed to be closer.

I try to solve it by sorting the bodies by the z-position. The bodies array is an array of objects with 0 containing an array of the body's position, 0 being x, 1 being y, 2 being z. I first have the position of the bodies updated according to rotations in the x, y, and z axes, stored into the np value of the body object, then do the sorting, and draw the bodies. I've tried changing how the array is sorted, changing the order of the loop, but still no cigar.

Just wondering if anyone can point me in the right direction to get this 3D "engine" behaving correctly. Any help is appreciated. Some quick notes: Rotations along the three axes are accomplished using the Q/A, W/S, and E/D keys, zooming in and out of the z-axis is accomplished using the R/F keys, and the default rotation about the z-axis can be accomplished using the P key. What I'm trying to do is located here:

http://jsbin.com/aholu/5/

A: 

You're sorting on the original xyz values instead of the transformed np values. I got it to look right by switching...

    bodies.sort(function(a,b) {return a[0][2]-b[0][2]});
to
    bodies.sort(function(a,b) {return a.np[1]-b.np[1]});

with change see http://home.comcast.net/~trochoid/mod5.html

I don't follow all of your code so this may not be a total solution. Specifically, I thought it'd be sorted on np[2] for the transformed z value, but np[1] gives correct looking results. I guess maybe you switch these coords. Also, it looks like you're transforming and projecting the z value and the code fix above sorts on this projected z value. It seems to work out ok but I've never projected the z value itself, just use the transformed z to project xy. Anyways, Looks good!

Trochoid
Wow, I guess that was just a brain fart on my part. I had added np (new position) as an attribute to the body object exactly so that it can have this rotated position to get which bodies are deeper. I guess another set of eyes was what I needed.About switching the coords, that was a result of me changing around indexes on the body's position to see which one would give the the correct result on depth. I'll probably go back and fix those to make it more readable.Thanks again!
Bob
Cool. I like it.
George
A: 

Hello again, Trochoid. I had to access from a different computer and still haven't registered, so I have to add this as an answer instead of a comment.

If you notice, the axis of rotation for x and y are rotated when you rotate any other axis (eg. if you press W to rotate, then rotate a different axis, then press W again, you will notice that the helix in your code rotates the same way). T

his is not so with the z axis. No matter how you rotate the x and y axes, the z axis will always rotate "right-to-left" (eg. in default configuration, helix is spiraling along z-axis, or axis rotated using the E/D keys, but if you rotate the helix along any other axis, rotating using the E/D keys no longer spirals the helix).

I don't know why the behavior would be different in that axis, so I would like your assistance to get that rotation working properly. Thank you.

bob