Fall and landing physics

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent cf17d444b0
commit fe96cb8df0
  1. 16
      Board.cpp
  2. 7
      Board.h
  3. BIN
      C++ProjectTemplate
  4. 2
      Meteos.h
  5. 39
      main.cpp

@ -3,8 +3,20 @@
extern Meteos*game;
Board::Board(vi2d boardSize,float gravity)
:boardSize(boardSize),gravity(gravity){
Board::Board(vi2d boardSize,float gravity,float spawnRate)
:boardSize(boardSize),gravity(gravity),spawnRate(spawnRate){
drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12};
yBottom=(boardSize.y-1)*12;
for (int i=0;i<boardSize.x;i++) {
cols.push_back(std::vector<Block>());
}
}
void Board::spawnBlock(int col){
Block newBlock=Block({(float)col*12,-120});
cols[col].push_back(newBlock);
}
std::vector<Block>&Board::getBlocks(int col){
return cols[col];
}

@ -1,14 +1,19 @@
#ifndef BOARD_H
#define BOARD_H
#include "pixelGameEngine.h"
#include "Block.h"
class Board{
std::vector<std::vector<Block>> cols;
public:
float yBottom;
vf2d drawOffset;
vi2d boardSize;
float gravity;
float spawnRate;
Board(){}
Board(vi2d boardSize,float gravity);
Board(vi2d boardSize,float gravity,float spawnRate);
void spawnBlock(int col);
std::vector<Block>&getBlocks(int col);
};
#endif

Binary file not shown.

@ -1,7 +1,6 @@
#ifndef METEOS_H
#define METEOS_H
#include "pixelGameEngine.h"
#include "Block.h"
#include "Board.h"
#include <random>
@ -10,7 +9,6 @@ class Meteos : public olc::PixelGameEngine{
Meteos(){
sAppName="Meteos";
}
std::vector<Block> blocks;
float lastBlockSpawn=0.0f;
std::uniform_int_distribution<> randBlockPos;
std::mt19937 gen;

@ -13,16 +13,16 @@ bool Meteos::OnUserCreate()
gen=std::mt19937(rd()); //Standard mersenne_twister_engine seeded with rd()
randBlockPos=std::uniform_int_distribution<>(0, 9);
gameBoard=Board({10,14},6.f);
gameBoard=Board({10,14},12.f,3.0f);
return true;
}
bool Meteos::OnUserUpdate(float fElapsedTime)
{
lastBlockSpawn+=fElapsedTime;
if (lastBlockSpawn>=5.0f){
lastBlockSpawn-=5.0f;
blocks.push_back(Block({(float)randBlockPos(gen)*12,0}));
if (lastBlockSpawn>=3.0f){
lastBlockSpawn-=3.0f;
gameBoard.spawnBlock(randBlockPos(gen));
}
Clear(Pixel(32,32,255));
for (int x=-1;x<=gameBoard.boardSize.x;x++) {
@ -34,15 +34,30 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
}
}
}
for (Block&b:blocks) {
if (b.pos.y<gameBoard.yBottom) {
b.vspeed+=gameBoard.gravity*fElapsedTime;
b.pos.y+=b.vspeed*fElapsedTime;
} else {
b.vspeed=0;
b.pos.y=gameBoard.yBottom;
for (int i=0;i<gameBoard.boardSize.x;i++) {
for (int y=0;y<gameBoard.getBlocks(i).size();y++) {
Block&b=gameBoard.getBlocks(i)[y];
if (b.pos.y<gameBoard.yBottom) {
b.vspeed+=gameBoard.gravity*fElapsedTime;
bool collisionDetected=false;
for (int yy=y-1;yy>=0;yy--) {
Block&b2=gameBoard.getBlocks(i)[yy];
if (b.pos.y+12>b2.pos.y) {
collisionDetected=true;
b.vspeed=0;
b.pos.y=b2.pos.y-12;
break;
}
}
if (!collisionDetected) {
b.pos.y+=b.vspeed*fElapsedTime;
}
} else {
b.vspeed=0;
b.pos.y=gameBoard.yBottom;
}
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
}
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
}
return true;
}

Loading…
Cancel
Save