Skip to content

Commit

Permalink
chapter 2 example 6
Browse files Browse the repository at this point in the history
  • Loading branch information
mimetaur committed Jan 4, 2013
1 parent 95308a2 commit d649709
Show file tree
Hide file tree
Showing 11 changed files with 583 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chapter02/Example6/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 )
48 changes: 48 additions & 0 deletions chapter02/Example6/src/Example6App.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "cinder/app/AppBasic.h"
#include "cinder/gl/gl.h"
#include "cinder/Rand.h"
#include "Attractor.h"
#include "Mover.h"


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

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

MoverRef mMover;
AttractorRef mAttractor;
};

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

void Example6App::setup()
{
mMover = MoverRef(new Mover());
mAttractor = AttractorRef(new Attractor());
}

void Example6App::update()
{
Vec2f force = mAttractor->attract(mMover);
mMover->applyForce(force);
mMover->update();
}

void Example6App::draw()
{
gl::clear( Color( 1, 1, 1 ) );

mAttractor->draw();
mMover->draw();
}

CINDER_APP_BASIC( Example6App, RendererGl )
42 changes: 42 additions & 0 deletions chapter02/Example6/xcode/Attractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Attractor.cpp
* Example6
*
* Created by Nathan Koch on 1/4/13
* Copyright 2013 __MyCompanyName__. All rights reserved.
*
*/

// header files
#include "cinder/app/AppBasic.h"
#include "cinder/CinderMath.h"
#include "Mover.h"
#include "Attractor.h"

// namespaces
using namespace ci;


Attractor::Attractor( ) {
mLocation = (Vec2f)app::getWindowCenter();
mMass = 20.0f;
mG = 0.4f;
}


void Attractor::draw( ) {
gl::color(0.68, 0.68, 0.68);
gl::drawSolidCircle(mLocation, mMass*2);
gl::color(0, 0, 0);
gl::drawStrokedCircle(mLocation, mMass*2);
}

Vec2f Attractor::attract(MoverRef m) {
Vec2f force = mLocation - m->mLocation;
float distance = force.length();
distance = math<float>::clamp(distance, 5.0f, 25.0f);
float strength = (mG * mMass * mMass) / ( distance * distance );
force.safeNormalize();
force *= strength;
return force;
}
25 changes: 25 additions & 0 deletions chapter02/Example6/xcode/Attractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Attractor.h
* Example6
*
* Created by Nathan Koch on 1/4/13
* Copyright 2013 __MyCompanyName__. All rights reserved.
*
*/
#include "Mover.h"
#pragma once

typedef std::shared_ptr<class Attractor> AttractorRef;
class Attractor {

public:

Attractor();

void draw();
ci::Vec2f attract(MoverRef m);

float mMass;
float mG;
ci::Vec2f mLocation;
};
Binary file added chapter02/Example6/xcode/CinderApp.icns
Binary file not shown.
Loading

0 comments on commit d649709

Please sign in to comment.