Onedayitwillmake

NYC Programmer Mario Gonzalez's blog

Model View Controller in the real world

I was watching some of the Apple WWDC 2010 videos, and these two images was shown.
They aren’t meant to be funny, but they made me laugh, it reminds me of when you’re trying to create the proper MVC paradigm on an actual application, and you keep wondering – damn do i really have to connect all these things just to show this info?

MVC In Concept

MVC In The Real World

-
Least im not the only one who wrestles and questions own choices of is this MVC or is it just spaghetti.
It’s both, but the idea is that its less spaghetti after you take a step back from implementation details

Post-Of-The-Day #7 – Some links?

Beautiful

MIRAGES from Sabrina Ratté on Vimeo.

Also Beautiful

Between Bears from Eran Hilleli on Vimeo.

My own little thing:

Could Never Reach You from mario gonzalez on Vimeo.


My friend told me I should do videos for Chemical Brothers, and linked me to this – which i thought was so amazing.

The Chemical Brothers – Another World from ikkunapaikka on Vimeo.

I’m going to make something with that audio, it reminds me a lot of this thing i made.
Except that theirs is WAY better

Linewaves from mario gonzalez on Vimeo.


So i’ve been doing some face tracking stuff…. and i accidentally created some pretty linewaves.
I think I will try to combine the two. My face tracking stuff is still so primitive, but we’ll see what i can fake.

Post-A-Day #4 – Objective-C Flocking-Behaviors/Boid Class

This post a day thing is great, it’s really made me kick some stuff into gear.

I love flocking behaviors, they’re awesome.
What are Flocking Behaviors?


They were created in the 1980′s from by Criag Renolds, the gist of it is that using 3 simple behaviors, surprisingly complex motion can be formed when you have many actors ( Referred to as Boids, i think Craig meant it as a another way of saying Birds, a more new york way).


The three rules are simple, i’ll explain them as i know them but im no expert.
Separation: Boids want to stay a little away from each other, some elbow room.
Cohesion: Boids want to be grouped, as long as they dont get too close they violate rule 1
Alignment: Boids within a certain range, care about which way other boids are going.

Simple right? Anyway, it makes for very pretty stuff.


A few months ago a friend of mine gave a talk based on Kieth Peters book AdvancED ActionScript 3.0 Animation (a great book). This re-sparked my interest in them, and with my on going interest in iphone development – I decided to try and port Kieth Peters code to Objective-C as best I could with the more limited knowledge I had at the time.

It actually didn’t work all that great, but, I came across SoulWire’s interpretation of Flocking (also in AS3), and I ported that. Maybe it was because it was my second attempt at porting, but this time I got really great results.
200 Objects flocking, all aware of every single other one, on a tiny iphone in your hand, that was very rewarding.
The problem was that I used Box2D’s point class B2Vec2, and then i modified it a little to boot, because I wanted to add a few operators it didn’t have built in. So it left me with something I could not really share with anyone else, and also it was now Combining Objective-C and C++, which always seems like you should avoid it whenever possible.

This was many months ago (march according to my SVN), and i had my fun playing with it and left it at that.
However, recently on the Cocos2D forums someone brought up making a heat seeking missile and I mentioned that stearing behaviors would be a great for that, if maybe over complicated but being a game forum – you worry about that less as it might be a great jumping platform from which additional gameplay ideas stem.

I decided I would revisit my class, as I had been wanting to for a long time, and re-write it using only CGPoints so that it could be pure Objective-C.

This was the result:

LittleBirds from mario gonzalez on Vimeo.

Here’s how to use the class:

 
// Creating it
boid = [Boid spriteWithSpriteSheet:_sheet rect: boidRect];
[boid setSpeedMax: 2.0f andSteeringForceMax: 1.0f];
[boid setWanderingRadius: 16.0f lookAheadDistance: 40.0f andMaxTurningAngle:0.2f];
 
// On your update function
while(boid)
	{
		Boid* b = boid;
		boid = b->_next;
		[b wander: 0.19f];
		[b 
		 flock:_flockPointer
		 withSeparationWeight:0.6f
		 andAlignmentWeight:0.1f
		 andCohesionWeight:0.2f
		 andSeparationDistance:10.0f
		 andAlignmentDistance:30.0f
		 andCohesionDistance:20.0f
		 ];
 
		[b flee:badThingPosition panicAtDistance:5 usingMultiplier:0.6f]; // avoid touch
		[b seek:yummyFoodPosition withinRange:75 usingMultiplier:0.35f]; // go towards touch
                [b update];
	}

The class may be downloaded here:
BoidsExample Version 0.2

It’s coded to the best of my ability, if you have (impactful) optimizations please share.

Update #1:
Fixed Flocking example class linked list creation bug.

Simple rules combine to make seemingly complex behaviors:

Accident – Linewaves

This is a pretty cool still frame – of an application I’m working on at the moment.
Changing this single parameter ( which controls acceleration ) – to a number i only accidentally set it to gave me this:


( You have to click on these to actually see what it looks like, the thumbnail scaling hides the effect )


Linewaves from mario gonzalez on Vimeo.

Post-A-Day

Hi

I’m going try my hand at making 1 post each day for the next month.
They will either exist here, or on my other blog (related to iphone development).

When I post on 1 I will cross post it here –

How to trace something in C++

Coming from an Actionscript background, i’ve become accustomed to being able to at will at any given point
tracing an object, or a value. It’s something i actually took for granted.

When I started using Cinder ( http://libcinder.org ) – and in the process doing C++. A language
at the time I had only barely used – but now i love. I realized that – trace, or speaking to my PHP background
printf is something that is not actually built in at all.

It’s something you have to turn into a c_string, which is basically a char* as i understand it.
Devnote: What is a Char* – it’s a pointer to the first character in a string of character, whose ASCI values ( 0 – 255 ) are mapped to strings.

So,
How do you trace a value in C++, be it an interger, string, float, double, or class?

In C++ ( and C? ) – it’s not as straigh forward, but fortunately we have something called operators.
An operator, is actualyl something you’re very accustomed to, such as:

+
-
*
/

Those are actually operators, and what does that mean? Well it means you can define one and when you’re
Type, comes across it it will run this function. We’ll get into it another time, cus its a great topic, but…
What that means right now, is that we can define a “<<" operator, so we can turn out data into a C-String with the relavent info.

So we can say:

friend std::ostream& operator<<( std::ostream& lhs, const Vec2& rhs )
{
lhs << "[" << rhs.x << "," << rhs.y << "]";
return lhs;
}

Which will make our console function return useful info.
For example:

console << Vec2f(10.0f, 11.1f) << std::endl;

One thing to note is that std::endl, what the heck is that?
It just means go into the STD namespace ( or package if you wanna use a different term )
and grab the "endl" constant... put that there.

Also - for POD ( Plain Old Data ) - we can call a toString function such as

template inline std::string toString (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}

Which can be used as:

float pie = 3.1415
Console() << toString( pie ) << std::endl;

And that's how you get values out into the console in C++ / Cinder.

Shipping data visualization

I was tasked with the idea of creating some proof-of-concept (I notice i do a lot of those, imo they’re the most fun part of the project) data-visualizations involving the movement of objects on an international scale. You can obviously tell i’m trying to give out as few specific details as possible that could get me into trouble, so sorry for being so seemingly mysterious.

The first concept I had was to take the data, and use various classic key elements of infographics to denote and show at a glance the voluminous information displayed. One of the problems I came across was that I basically had more data than my computer could handle, so I had to truncate it, but that left me with information that heavily favored specific ports. In the end I used Perlin Noise to generate realistic feeling randomness to these proof-of-concepts.

(Sorry. Again, cant get into specifics, but this might be as close as this project gets to getting made so i thought i’d share.)
The key is as follows:
X axis represents something
Y axis represents a different value Logarithmicly scaled
Size is controlled by weight
Color is controlled by destination country


(more…)