Spline Driver to be Open Source

/* Unity Project */
I decided today that I should dig up an old project mostly for nostalgia. It occurred to me that some people might want to take this code and do something useful with it. Therefore, as soon as I get enough time I will revise the code and release it under an open-source license. I am strongly considering the GPL, as I want it to be a community effort without too much commercial intent. Before that happens I will need a vehicle model, as the one I used cannot be distributed. I would also like to add sound, but that is not very difficult.


Stop staring at the screenshots and go play.

Play Spline Driver

My Plan

UPDATE 05/21/2012: Woot! Just finished a gear joint example. Check it out on the usual joints page.

UPDATE 05/21/2012: Mouse, Distance, Revolute, Pulley, and Prismatic Joint (example code) added to Chapter 9 – Joints.

UPDATE 05/19/2012: Chapters 1, 2, 3, 4, 5, and 6 of Box2D now have a rough outline in place! The posts that were previously associated with those chapters have been moved into their proper location. The loose content remains on the home page.

I am thinking of restructuring this site, as it is getting messy pretty quick. My plan is to create something like an online textbook, or at least an extremely detailed view of all the documentation available on Box2D, Unity 3D, and the UDK. Please check out my Box2D tab, to see the outline I have created. I would really appreciate some input, but I will be working on filling those pages regardless. Eventually, all the content on the Home page will be converted into an About This Site page. I will leave up these posts until they are fully incorporated into the Box2D tab. Please bookmark my site and check back every once in a while. I really want this site to help others, and reader feedback is the only way that is going to happen. I look forward to any comments, suggestions, or hate mail. Thanks for your patience.

Feedburner Problems

I assume that many of my subscribers left because my Feedburner feed went down yesterday. Unfortunately there is a limit of 512kb which I was not aware of. From now on, I will be using the default WordPress RSS feed. Hopefully those who left will understand, and will not dismiss my site. I plan on developing this website for the next several years, and I hope that it continues to be useful to everyone who wants to start making games without ripping their hair out.

Apply Impulse in Box2D

Here is an example of the “ApplyImpulse” function in Box2D. This code will not work for a platform game, because the player could easily jump straight up a wall. In a future tutorial I will describe how platform games accomplish jumping.

package
{
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.*;
	import Box2D.Dynamics.Contacts.b2ContactEdge;
	import Box2D.Collision.Shapes.b2PolygonShape;
 
	public class Main extends MovieClip
	{
		var world:b2World;
		var body:b2Body;
		var bodyDef:b2BodyDef = new b2BodyDef;			
		var fixtureDef:b2FixtureDef = new b2FixtureDef;
 
		var timeStep : Number = 1.0 / 60.0;
		var velocityIterations:int = 6;
		var positionIterations:int = 2;
 
		var keyboardUp:Boolean = false;
		var keyboardLeft:Boolean = false;
		var keyboardRight:Boolean = false;
		var jumping:Boolean = false;
 
		public function Main()
		{
			var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
			var doSleep:Boolean = true;
			world = new b2World(gravity, doSleep);
 
			var groundBodyDef:b2BodyDef = new b2BodyDef();
			groundBodyDef.position.Set(0.0, 20.0);			
			var groundBody:b2Body = world.CreateBody(groundBodyDef);
			var groundBox:b2PolygonShape= new b2PolygonShape();
			groundBox.SetAsBox(50.0, 10.0);
			groundBody.CreateFixture2(groundBox);
 
			bodyDef.type = b2Body.b2_dynamicBody;
			bodyDef.position.Set(9.0, 4.0);
			body = world.CreateBody(bodyDef);
			var dynamicBox:b2PolygonShape = new b2PolygonShape;
			dynamicBox.SetAsBox(1.0, 1.0);
			fixtureDef.shape = dynamicBox;
			fixtureDef.density = 1.0;
			fixtureDef.friction = 0.3;
			body.CreateFixture(fixtureDef);
			body.SetBullet(true);
 
			var sprite:Sprite = new Sprite();
			var dbgDraw:b2DebugDraw = new b2DebugDraw();
			dbgDraw.SetSprite(sprite);
			dbgDraw.SetDrawScale(30.0);
			dbgDraw.SetFillAlpha(0.3);
			dbgDraw.SetLineThickness(1.0);
			dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
			world.SetDebugDraw(dbgDraw);
			addChild(sprite);
 
			addEventListener(Event.ENTER_FRAME, update, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}
 
		public function update(event:Event):void
		{
			world.DrawDebugData();
			var position:b2Vec2 = body.GetPosition();
			var angle:Number = body.GetAngle();
			var linearVelocity:b2Vec2;
 
			if(keyboardUp)
			{
				var contactList:b2ContactEdge = body.GetContactList();
				if(contactList != null)
					body.ApplyImpulse(new b2Vec2(0.0, -3.0), body.GetWorldCenter());
			}
			if(keyboardLeft && !keyboardRight)
			{
				body.SetAwake(true);
				linearVelocity = body.GetLinearVelocity();
				body.SetLinearVelocity(new b2Vec2(-5, linearVelocity.y));				
			}
			if(keyboardRight && !keyboardLeft)
			{
				body.SetAwake(true);
				linearVelocity = body.GetLinearVelocity();
				body.SetLinearVelocity(new b2Vec2(5, linearVelocity.y));				
			}
			world.Step(timeStep, velocityIterations, positionIterations);			
			//var str:String = Number(position.x).toFixed(2) + " ";
			//str += Number(position.y).toFixed(2) + " ";
			//str += Number(angle).toFixed(2);
			//trace(str);
		}
 
		private function keyDownHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.LEFT)
				keyboardLeft = true;
			else if(event.keyCode == Keyboard.RIGHT)
				keyboardRight = true;
			else if(event.keyCode == Keyboard.UP)
				keyboardUp = true;
		}
 
		private function keyUpHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == 82)
			{
				// Reset
				world.DestroyBody(body);
				body = world.CreateBody(bodyDef);
				body.CreateFixture(fixtureDef);
			}
			else if(event.keyCode == Keyboard.UP)
				keyboardUp = false;
			else if(event.keyCode == Keyboard.RIGHT)
				keyboardRight = false;
			else if(event.keyCode == Keyboard.LEFT)
				keyboardLeft = false;
		}
	}
}

Try jumping by tapping or holding the up arrow key. Thank you for visiting and thank you to all who have subscribed. If you have tutorial suggestions please send them my way :)

Download the Complete Source