Posts Tagged ‘cocos2d’

Here’s the deal. I wrote a bit of sample code to generate 2D terrain. (See here and here) A fellow blogger and reader pointed out a performance flaw to me and suggested I do it differently. So I did. With problems.

I’m usually posting tips and help to readers, and it’s your turn to return the favor, if you would be so kind. Find the flaw, win an ‘atta boy, pat on the back, and a thank you. Sorry, no prizes. (more…)

Here is some iOS code that allows you to generate your own terrain in a 2d iPhone side-scrolling style game by simply dragging your finger up and down. The possible potential uses are endless. This code uses Cocos2D.




(more…)

Ask any developer of fast-paced iPhone games what language they use and none will say Objective-C. I am not referring to card games, or simple games like SpaceBubble. I am talking about processor intensive, special effects, things flying at you from all angles, kind of games. Of course every iPhone game has to use SOME Objective-C to get the app up and running and for certain API calls, but those kinds of things should not take up more than a few hundred lines of code at most in a large game.
(more…)

Here is some example code based off my tutorial on how to implement orbital / radial gravity, but in reverse. This example pushes objects away instead of pulling inward. It makes for a nice explosion effect. Much work would need to be done to make it realistic, but you get the general idea of how to blow things up. You need to have Cocos2D and Box2D installed. Box2D comes packaged with the latest version if Cocos2D.




Download Source Code for this tutorial:
Bomb Demo Box2d & Cocos2d Example Source Code – 799k

The other day I wrote a tutorial on how to implement orbital / radial gravity with the use of Box2D and Cocos2D. I have decided to post the source code for this. You need to have Cocos2D and Box2D installed. Box2D comes packaged with the latest version if Cocos2D.



Download Source Code for this tutorial:
Radial Gravity Box2d & Cocos2d Example Source Code – 692k

Radial gravity, orbital gravity, orbital mechanics, whatever you want to call it there is no denying it can be useful for many kinds of games. Unfortunately, neither Box2D nor Chipmunk Physics Engines for the iPhone had support for it. I had a hell of a time trying to figure this out but in the end it was so simple. Here is how to do it.

First, create a new Cocos2D / Box2D Hello World project from the supplied Cocos2D templates. Add the following code to HellowWorldScene.mm and HelloWorldScene.h:

HelloWorldScene.mm

-(id) init {
//... Add at end of init after body->CreateFixture(&fixtureDef);
	// Create our static "Planet"
	b2CircleShape shape;
	shape.m_radius = 1.0f;
	shape.m_p.Set(8.0f, 8.0f);
	b2FixtureDef fd;
	fd.shape = &shape;
	planet = groundBody->CreateFixture(&fd);
}
 
-(void) tick: (ccTime) dt
{
	int32 velocityIterations = 8;
	int32 positionIterations = 1;
 
	world->Step(dt, velocityIterations, positionIterations);
 
	for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
	{
		b2Body* ground = planet->GetBody();
		b2CircleShape* circle = (b2CircleShape*)planet->GetShape();
		// Get position of our "Planet"
		b2Vec2 center = ground->GetWorldPoint(circle->m_p);
		// Get position of our current body in the iteration
		b2Vec2 position = b->GetPosition();
		// Get the distance between the two objects.
		b2Vec2 d = center - position;
		// The further away the objects are, the weaker the gravitational force is
		float force = 250.0f / d.LengthSquared(); // 150 can be changed to adjust the amount of force
		d.Normalize();
		b2Vec2 F = force * d;
		// Finally apply a force on the body in the direction of the "Planet"
		b->ApplyForce(F, position);
 
		if (b->GetUserData() != NULL) {
			CCSprite *myActor = (CCSprite*)b->GetUserData();
			myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
			myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
		}	
	}
}

HelloWorldScene.h

@interface HelloWorld : CCLayer
{
	b2World* world;
	GLESDebugDraw *m_debugDraw;
	b2Fixture* planet; // This will be a static body
}

And here is the result!

Enjoy!

Download Source Code for this tutorial:
Radial Gravity Box2d & Cocos2d Example Source Code – 692k

Game Concept Progress

Still not going to give away any details on the game this soon, BUT here is another video teaser of my iPhone game using Cocos2D and Box2D and a ton of radial gravity. :)

There are no fancy graphics yet, and won’t be for quite some time.

iPhone Game Proof of Concept

I am working on a new iPhone game. It will be a physics-based puzzle game involving a maze-like playing field and lots and lots of crazy gravity. I won’t give away too many details though. ;)

After multiple headaches and thinking it may be harder than it’s worth to implement the physics involved with radial gravity, I stumbled across Cocos2d and Box2D. I was impressed with the linear gravity but somewhat disappointed to find there was no support for radial gravity forces so I had to code up an implementation on top of the existing framework.

Thanks to some tips from a fellow software developer I have a working representation of orbital mechanics at its finest.

Applied a variation of the Inverse Square Law for the radial gravity.

(more…)

Twitter Me