Use arrow keys to control paramecium.
Keep an eye on this one, I'm going to be updating it several times. I just wanted to go ahead and put the first version out there. The idea on this project was to create something that moved a little bit like an amoeba or a paramecium might. In my mind that meant something that had a slightly larger head than tail, that was jointed in the middle, and that head a "tail" that sort of trailed along behind it in a blob like fashion.
So, to get things started I created a "bone" class. This class consists of basically two stored points with a line drawn between them, and a spring and friction setting. I added a couple of small boxes on the end of each bone just to make the joints more visible. There a several methods that can be used to animate each bone. The ends can be set directly, you can use a "pull" method in which one end is set directly (usually an anchor) and the rotation is a function of it's current rotation compared to it's anchor object's rotation, with spring and friction applied.
GeSHi © 2004, Nigel McNie
- public function PullBone(sx:Number, sy:Number, sr:Number):void
- {
- SetRotation();
- var r:Number = sr - boneRotation;
- if (r < -180) { r = r + 360; }
- if (r > 180) { r = r - 360; }
- var ra:Number = r * (spring);
- rv += (ra);
- rv *= (friction);
- boneRotation += rv;
- x1 = sx;
- y1 = sy;
- x2 = sx + (Math.cos(boneRotation * Math.PI / 180) * initialLength);
- y2 = sy + (Math.sin(boneRotation * Math.PI / 180) * initialLength);
- SetBone(x1,y1,x2,y2);
- }
P.S. the code above is highlighted using GeSHi, which seems pretty cool so far.
If you look at the code above, you can see that it compares the difference in rotation between the bone and it's parent (anchor) which is passed in as "sr". We normalize that done to a number no greater than 180 and no less than -180. That's important to keep from getting some strange effects when the angles of either object change from 360 to 1 or vice versa. After that, spring is applied based on the difference in rotation, which will cause the bone to respond more strongly when it is farther out of line with it's anchor. Friction is used to slow the reactions over time.
The bone class is used by another class called "Paramecium". This class builds the little fella you see on screen using bones and a couple of circles. There isn't much magic in him. Most of it is in the bone class. The next step will be to add a couple of cross bones, or ribs, to the paramecium. I'll use those to curve a line around the skeleton I've built and fill it with an appropriate color or pattern. I can then add a couple of highlight graphics based on the circle or rib positions to make it look a little more "real", although photo realism is certainly not what I'm going for here. I just want something that would be appropriate for a game hero or villain.
Mostly what I wanted to do was to go ahead and get this beginning file uploaded. I'll refresh it soon and possibly change this post drastically as I do. In the end I think I'll get this guy doing exactly what I want, then translate into Silverlight to build a game around. I'm thinking Silverlight on this one because I've built tons of games in flash (and Shockwave, for those of you old enough to remember that) but I haven't built one in Silverlight yet.
Stay tuned, more to come...
No comments:
Post a Comment