Skip to content

Commit

Permalink
chapter 2 exercise 5
Browse files Browse the repository at this point in the history
  • Loading branch information
mimetaur committed Jan 3, 2013
1 parent 6977b08 commit 616fe15
Show file tree
Hide file tree
Showing 11 changed files with 634 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chapter02/Exercise5/include/Resources.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include "cinder/CinderResources.h"
#include "Resources.h"

//#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE )
74 changes: 74 additions & 0 deletions chapter02/Exercise5/src/Exercise5App.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "cinder/app/AppBasic.h"
#include "cinder/gl/gl.h"
#include "cinder/Rand.h"
#include "Mover.h"
#include "Liquid.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class Exercise5App : public AppBasic {
public:
void setup();
void update();
void draw();
void prepareSettings(Settings *settings);

vector<Mover*> mMovers;
int mNumMovers;

Liquid *liquid;
};

void Exercise5App::prepareSettings(Settings *settings) {
settings->setFullScreen();
}

void Exercise5App::setup()
{
mNumMovers = 20;
int i = 0;
while (i < mNumMovers) {
mMovers.push_back(new Mover(randFloat(1.0, 5.0), randFloat(getWindowWidth()), randFloat(getWindowHeight()/2)));
i++;
}
liquid = new Liquid();
}

void Exercise5App::update()
{
Vec2f wind = Vec2f(0.05, 0);
for (auto mover : mMovers) {
if (liquid->contains(mover->mLocation)) {
mover->drag(liquid);
}

mover->applyForce(wind);

float c = 0.01;
Vec2f friction = mover->mVelocity;
friction *= -1;
friction.safeNormalize();
friction *= c;
mover->applyForce(friction);

float m = (0.1 * mover->mMass);
Vec2f gravity = Vec2f(0, m);
mover->applyForce(gravity);

mover->update();
mover->checkEdges();
}
}

void Exercise5App::draw()
{
gl::clear( Color( 1, 1, 1 ) );
liquid->draw();
for (auto mover : mMovers) {
mover->draw();
}
}

CINDER_APP_BASIC( Exercise5App, RendererGl )
Binary file added chapter02/Exercise5/xcode/CinderApp.icns
Binary file not shown.
Loading

0 comments on commit 616fe15

Please sign in to comment.