Gerard-Meier

Basic Boid

Feb 28th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <nds.h>
  2. #include <vector>
  3.  
  4. #include "Application.h"
  5. #include "Vector2D.h"
  6.  
  7. #include "Boid.h"
  8. #include "Graphics.h"
  9.  
  10. using namespace std;
  11.  
  12. Boid::Boid(Application* argOwner, int argX, int argY) { //
  13.     // Reference to the owning application:
  14.     owner   = argOwner;
  15.    
  16.     // Physics related vectors:
  17.     lastPosition    = new Vector2D(argX, argY);
  18.     position        = new Vector2D(argX, argY);
  19.     velocity        = new Vector2D(1, 0);
  20.    
  21.     setColor(RGB15(31,31,31) | (1<<15));
  22. }
  23.  
  24.  
  25. void Boid::update(void) {
  26.     // Store the current position so we can use verlet integration lateron:
  27.     lastPosition->x = position->x;
  28.     lastPosition->y = position->y;
  29.    
  30.     // Basic motion:
  31.     position->x += velocity->x;
  32.     position->y += velocity->y;
  33.    
  34.     // Flip the velocity if the screenbounds are reached (yet to impletement resolveLocation)
  35.     if(position->x >= SCREEN_WIDTH  || position->x <= 0) velocity->x *= -1;
  36.     if(position->y >= SCREEN_HEIGHT || position->y <= 0) velocity->y *= -1;
  37. }
  38.  
  39. void Boid::draw(Graphics* g) {
  40.     unsigned int collisionCount = 0;
  41.     Vector2D force;
  42.    
  43.     // Get all the boids in this application:
  44.     vector<Boid*>* allBoids = owner->getBoids();
  45.    
  46.     for(unsigned int i = 0; i < allBoids->size(); i++) {
  47.         if(this != allBoids->at(i)) {
  48.             if(allBoids->at(i)->getPosition()->distanceTo(position) < 50) {
  49.                 collisionCount++;
  50.                 force.x += allBoids->at(i)->getVelocity()->x;
  51.                 force.y += allBoids->at(i)->getVelocity()->y;
  52.             }
  53.         }
  54.     }
  55.    
  56.     if(collisionCount > 0) {
  57.         force.x *= 1 / collisionCount;
  58.         force.y *= 1 / collisionCount;
  59.        
  60.         if(force.getLengthSQ() > 1) {
  61.             force.normalize();
  62.            
  63.             force.x *= 1;
  64.             force.y *= 1;
  65.         }
  66.        
  67.         velocity->x += force.x;
  68.         velocity->y += force.y;
  69.        
  70.         setColor(RGB15(31,0,0) | (1<<15));
  71.     } else {
  72.         printf("\x1b[%i;0H %f ", 3, 0.0);
  73.         setColor(RGB15(31,31,31) | (1<<15));
  74.     }
  75.    
  76.     // Erase the old pixel (draw a black one on top)
  77.     //g->setColor(RGB15(0,0,0) | (1<<15));
  78.     //g->drawPixel(lastPosition->x, lastPosition->y);
  79.    
  80.     // Draw the new pixel!
  81.     g->setColor(color);
  82.     g->drawPixel(position->x, position->y);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment