Hey. I am new to Arduboy and I wanted to start out with a simple pixel collision simulator. I was wondering if I could get a bit of advice about the collision detection logic.
I have a Particle Class:
class Particle {
int x,y, dx, dy;
public:
void set_values(int,int, int, int);
int get_x();
int get_y();
int get_dx();
int get_dy();
};
#include "./Particle.h"
void Particle::set_values (int j, int k, int l, int n) {
x = j;
y = k;
dx = l;
dy = n;
}
int Particle::get_x () {
return x;
}
int Particle::get_y () {
return y;
}
int Particle::get_dx () {
return dx;
}
int Particle::get_dy () {
return dy;
}
Here is the Ino:
/*
* Collider Demo
* At some point... the particles should bounce off one another... or perhaps explode
* Use the Arduboy buttons to add and remove particles? Not sure how to make that dynamic in c...
*/
#include <Arduboy2.h>
#include <Particle.h>
Arduboy2 arduboy;
// define framerate and how many particles.
#define FRAMERATE 60
#define HOWMANYPARTICLES 10
// Define Screen Dimensions
#define H 62
#define W 128
// Bunch of integers.
int dx, dy, cX, cY, initX, initY, initDX, initDY;
// Make an array of particles.
Particle particle[HOWMANYPARTICLES];
void setup() {
// Seed random.
randomSeed(analogRead(0));
// Setup serial for debugging.
Serial.begin(115200);
// Standard Arduboy stuff.
arduboy.begin();
arduboy.setFrameRate(FRAMERATE);
// The goal of this loop is to randomly place the particles.
if (arduboy.everyXFrames(6)) {
for (int j = 0; j < HOWMANYPARTICLES; j++){
// get initial particle positions.
initX = random(0,W);
initY = random(0,H);
// get random initial particle speeds.
// These forces need to be a part of the object.
initDX = random(1,5);
initDY = random(-1,-5);
// debugging.
Serial.println(initX);
Serial.println(initY);
// set the particle speeds and speeds.
particle[j].set_values(initX, initY, initDX, initDY);
}
}
}
void loop() {
if(!arduboy.nextFrame())
return;
/*
* In short, this nested loop should iterate through each particle and "animate it"
*/
for (int j = 0; j < HOWMANYPARTICLES; j++){
cX = particle[j].get_x();
cY = particle[j].get_y();
dx = particle[j].get_dx();
dy = particle[j].get_dy();
// Used the logic from the following reference for bounds checking. If the particle reached the boundary set the dx value negative.
// Reference: https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript/Bounce_off_the_walls
if ((cX + dx > W) || (cX + dx < 0)){
dx = -dx;
}
if ((cY + dy > H) || (cY + dy < 0 )) {
dy = -dy;
}
// This should check if a the current particle shares the same location as the previous particle.
// Is there is less naive way to do this?
if (cX == particle[j-1].get_x() && cY == particle[j-1].get_y()){
dx = -dx;
dy = -dy;
// Debug, print when a collusion occurs.
Serial.println("Collision!");
}
cX += dx;
cY += dy;
arduboy.drawPixel(cX,cY);
particle[j].set_values(cX, cY, dx, dy);
//arduboy.display();
}
arduboy.display();
arduboy.clear();
}
The collision logic does produce output but the particles move to fast to see a vector change. Would this be a good place to do something with a sprite animation?
Thanks!