chipKIT® Development Platform

Inspired by Arduino™

My bouncing balls physic simulation code

Created Sat, 24 Sep 2011 23:16:19 +0000 by serveurperso


serveurperso

Sat, 24 Sep 2011 23:16:19 +0000

Need any graphic LCD + lib... Thanks to Henning for the library:)

http://www.youtube.com/watch?v=cpRBRRVFx_Q

with touch http://www.youtube.com/watch?v=fi8TAKrIojw

You can get a cheaper SSD1289 screen for 23$ on elecfreaks.com (3.2" TFT LCD Screen Module: TFT01-3.2) no shield is needed (all 3.3v), just jumper wires and a resistor to power the backlight from 5V pin. This lib http://www.henningkarlsen.com/electronics is very fast (direct port writing) and easy to use (give the pinout for chipkit AND elecfreaks screen)

#include <CK_ITDB02_Graph16.h>

#define tftX 240
#define tftY 320

#define n 10

#define ledPin 2

ITDB02 tft(82, 83, 84, 85, ITDB32S);

uint8_t i;
uint8_t j;

float x[n];
float y[n];
uint8_t r[n];

float oldX[n];
float oldY[n];

float velX[n];
float velY[n];

uint8_t red[n];
uint8_t green[n];
uint8_t blue[n];

void setup() {
 float circle;
 const float phase1 = 2 * PI / 3;
 const float phase2 = 4 * PI / 3;

 for(i = 0; i < n; i++) {
  x[i] = tftX / 2;
  y[i] = tftY / 2;
  r[i] = i * 2 + 10;

  oldX[i] = x[i];
  oldY[i] = y[i];

  velX[i] = 2;
  velY[i] = 2;

  circle = i * 2 * PI / n;
  red[i] = cos(circle) * 127 + 127;
  green[i] = cos(circle + phase2) * 127 + 127;
  blue[i] = cos(circle + phase1) * 127 + 127;
 }

 tft.InitLCD();
 tft.clrScr();

 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, HIGH);
}

void loop() {
 for(i = 0; i < n; i++) {

  x[i] += velX[i];
  y[i] += velY[i];

  for(j = i + 1; j < n; j++)
   collision();

  borders();

  if(int(x[i]) != int(oldX[i]) or int(y[i]) != int(oldY[i]))
   draw();

 }
}

void collision() {
 float disX = x[j] - x[i];
 float disY = y[j] - y[i];
 float d2 = disX * disX + disY * disY;

 if(d2 != 0) {
  float rij = r[i] + r[j];
  float rij2 = rij * rij;

  if(d2 < rij2) {
   float ii = (disX * velX[i] + disY * velY[i]) / d2;
   float ji = (disX * velY[i] - disY * velX[i]) / d2;
   float ij = (disX * velX[j] + disY * velY[j]) / d2;
   float jj = (disX * velY[j] - disY * velX[j]) / d2;
   float ratio = rij / sqrt(d2);

   velX[i] = ij * disX - ii * disY;
   velY[i] = ij * disY + ii * disX;
   velX[j] = ji * disX - jj * disY;
   velY[j] = ji * disY + jj * disX;

   disX *= (ratio - 1) / 2;
   disY *= (ratio - 1) / 2;

   x[j] += disX;
   y[j] += disY;
   x[i] -= disX;
   y[i] -= disY;
  }
 }
}

void borders() {
 if(x[i] >= tftX - r[i] - 1) {
  x[i] = tftX - r[i] - 1;
  velX[i] = -velX[i];
 } else if(x[i] <= r[i]) {
  x[i] = r[i];
  velX[i] = -velX[i];
 }

 if(y[i] >= tftY - r[i] - 1) {
  y[i] = tftY - r[i] - 1;
  velY[i] = -velY[i];
 } else if(y[i] <= r[i]) {
  y[i] = r[i];
  velY[i] = -velY[i];
 }
}

void draw() {
 tft.setColor(0, 0, 0);
 tft.drawCircle(oldX[i], oldY[i], r[i]);

 tft.setColor(red[i], green[i], blue[i]);
 tft.drawCircle(x[i], y[i], r[i]);

 oldX[i] = x[i];
 oldY[i] = y[i];
}

:mrgreen: