Tuesday, April 14, 2009

Papervision 3D + physics engine.

I've done several updates to the model which I haven't posted about. They are:

- Got rid of ShadowCaster.
- Added click behavior.

The reason I had to eliminate ShadowCaster was because it was downgrading performance too much when the stage was big. Because I am working with full screen stage it was a big issue.

Thanks to Andy Zupko who recommended a better alternative for shadows for what I was trying to do, and even provided the logic for calculating positioning based on light and sphere position. The new version has a plane with blurred circle baked texture material per sphere. Positioned at the bottom of the sphere and with the following logic from Andy to calculate position:

var vector: Number3D = new Number3D(m.x - light.x, m.y-light.y, m.z - light.z);
var spherePos:Number3D = new Number3D(m.x, m.y, m.z);
vector = Number3D.add(vector, spherePos);
this.position = vector;
m being each marble (sphere), and light, well... the light :s.

This runs on every frame as well because as the position of the sphere relative to the light position changes, so does the shadow obviously.

You'll also notice that I added onClick event handler as well finally. More on that on my next posting.

Oops! spheres too big? Visit the bigger site here.


Wednesday, April 8, 2009

Papervision 3D, ShadowCaster tweaks and ColorMatrixFilter Animation

In this version I "modified" (hacked would be a better word) Andy Zupko's ShadowCaster class because of the problems I was having using it (see my previous post):

Fixed (or hacked really) my problem with the non perFaceCasting, that is using the ShadowCaster.castBoundingSphere() method. My problem was that the shadow was being affected by the rotation of the sphere (see my previous post), so I changed the following in the ShadowCaster class:

var dx:Number = r2d.x-c2d.x;
var dy:Number = r2d.y-c2d.y;
var rad:Number = Math.sqrt(dx*dx+dy*dy);

var dx:Number = r2d.x-c2d.x;
var dy:Number = r2d.y-c2d.y;
var rad:Number = b.radius/1.1

Divided by 1.1 to make the shadow a little smaller than the radius. It’s not accurate but it works and hopefully somebody will address this in a more professional/consistent way.

I also added some some behavior to the mouse and collision events. For now just animated color using ColorMatrixFilter.

Bigger, stand-alone version here.

I also debugged the mouse generated forces (Ad nauseam) because I found some problems with it. Thanks Cesar Cintron for the feedback. A brief explanation is that I was canceling events from being dispatched by the mouse on the wrong side of the sphere. This happened when the mouse movement was very fast and so the event was dispatched late when the mouse had already passed the center of rotation and thus fired on the wrong side of the ball, making it shoot very fast in the opposite direction from that of the mouse movement.

Thursday, April 2, 2009

Casting Shadows in Papervision3D - ShadowCaster class

I've added shadows to my model today. Thanks to the amazing work of Andrew Zupko who did (and re-did) the classes for shadow casting in Papervision3D, I was able to integrate them very easily. I think this stand-alone class should make it into the trunk eventually.

I bumped into a problem though, when I setup the castPerFace parameter in the castModel method to true, it works perfectly. You can even see the shadow of the sides of the sphere which, in order to keep polygon count down, is not entirely smooth. You can see this "good" version below.

BUT, I don't really need per face casting, it adds some some processing to the already cpu intensive model and the non-per face casting method is perfect because it projects a bounding sphere of the model as a shadow. So given that my models are spheres, I could use the performance benefit of using the simpler shadow casting.

Here's the thing, shadows do weird things when setup without the per face casting. It's like the shadow is bouncing underneath the ball. Must surelly have something to do with the fact that it is turning but couldn't figure out the mathematics in the ShadowCaster class to be able to fix it, or at least play with it. What makes me think the it could be the ShadowCaster class and not the way I've implemented it (didn't include the clone hack for example) is because it works as expected with the per face casting.

I am going to leave it like that for now until I (or someone) finds a fix to this issue. This bad version can be found here.