#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_I2CDevice.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1, 888888); // Define Display
class PhysicsObject {
public:
int posx = display.width() / 2;
int posy = display.height() / 2;
int velx = 0; int vely = 0;
int accx = 0; int accy = 0;
int frcx = 0; int frcy = 0;
PhysicsObject() {
posx = (display.width() / 2) * 100;
posy = (display.height() / 2) * 100;
}
void update() {
velx += accx;
vely += accy;
velx *= (100 - frcx) / 100.0;
vely *= (100 - frcy) / 100.0;
posx += velx;
posy += vely;
if (posx >= (display.width() - 1) * 100 || posx <= 0) {
posx = max(0, min(posx, (display.width() - 1) * 100));
velx = -velx - (velx / 4);
}
if (posy <= 0 || (posy >= (100 * (display.height() - 1)))) {
posy = max(0, min(posy, (display.height() - 1) * 100));
vely = 0;
}
}
};
PhysicsObject obj;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Begin I2C Communication With Display
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
display.clearDisplay(); // Clear Display Buffer
display.setTextSize(1);
display.setTextColor(SSD1306_INVERSE);
display.setCursor(0, 0);
display.display(); // Display On Screen
obj.frcx = 5;
}
int jump = 0; int prevjump = 0;
void loop() {
jump = !digitalRead(9);
display.clearDisplay();
obj.accx = 0;
if (!digitalRead(8)) obj.accx = -10;
if (!digitalRead(10)) obj.accx = 10;
if (jump > prevjump) obj.vely = -300;
obj.accy = 15;
obj.update();
display.drawPixel(obj.posx / 100, obj.posy / 100, SSD1306_WHITE);
display.display();
prevjump = jump;
}