Collision detection (much better and finished)

This commit is contained in:
Pòsweg 2017-04-30 21:23:48 +02:00
parent 556ccd976e
commit e0508c2a44
6 changed files with 21 additions and 8 deletions

View File

@ -6,7 +6,7 @@ CC = g++
COMPILER_FLAGS = -w COMPILER_FLAGS = -w
LINKER_FLAGS = -lSDL2 -std=gnu++11 LINKER_FLAGS = -lSDL2 -std=gnu++11 -lSDL2_image
OBJ_NAME = bin/main OBJ_NAME = bin/main

BIN
bin/main

Binary file not shown.

View File

@ -2,10 +2,16 @@
#include"camera.h" #include"camera.h"
Camera::Camera(int mapWidth,int screenWidth){
mW = mapWidth;
scW = screenWidth;
}
void Camera::update(int playerX, int playerY){ void Camera::update(int playerX, int playerY){
//Make the camera when the player hits the middle of the screem //Make the camera when the player hits the middle of the screem
if(true) posX = playerX - 640/2; if(playerX >= scW/2) posX = playerX - scW/2;
//else posX = 0; else posX = 0;
if(playerX >= mW - scW/2) posX = mW - scW;
//posY = playerY; //posY = playerY;
}; };

View File

@ -5,11 +5,13 @@
class Camera{ class Camera{
public: public:
Camera(int mapWidth,int screenWidth);
void update(int playerX, int playerY); void update(int playerX, int playerY);
int getPosX(); int getPosX();
int getPosY(); int getPosY();
private: private:
int posX, posY; int posX, posY;
int mW, scW;
}; };
#endif #endif

View File

@ -2,6 +2,7 @@
#include<iostream> #include<iostream>
#include<SDL2/SDL.h> #include<SDL2/SDL.h>
//#include<SDL2/SDL_image.h>
#include"player.h" #include"player.h"
#include"block.h" #include"block.h"
#include"camera.h" #include"camera.h"
@ -19,9 +20,6 @@ void init();
bool loadMedia(); bool loadMedia();
void close(); void close();
Player posweg;
Camera camera;
bool loadMedia(){ bool loadMedia(){
bool success = true; bool success = true;
@ -52,11 +50,15 @@ int main(int argc, char* args[]){
bool quit = false; bool quit = false;
SDL_Event e; SDL_Event e;
Player posweg;
Camera camera(36*sz,SCREEN_WIDTH);
Block wallA(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,&gRenderer); Block wallA(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,&gRenderer);
Block wallB(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); Block wallB(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer);
Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer);
Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer);
Block ground2(SCREEN_WIDTH+sz*4,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer);
posweg.init(100,100,sz,sz,&gRenderer); posweg.init(100,100,sz,sz,&gRenderer);
@ -78,12 +80,14 @@ int main(int argc, char* args[]){
wallB.print(camera.getPosX()); wallB.print(camera.getPosX());
wallC.print(camera.getPosX()); wallC.print(camera.getPosX());
ground.print(camera.getPosX()); ground.print(camera.getPosX());
ground2.print(camera.getPosX());
posweg.check(wallA.getRectangle()); posweg.check(wallA.getRectangle());
posweg.check(wallB.getRectangle()); posweg.check(wallB.getRectangle());
posweg.check(wallC.getRectangle()); posweg.check(wallC.getRectangle());
posweg.check(ground.getRectangle()); posweg.check(ground.getRectangle());
posweg.check(ground2.getRectangle());
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
} }
close(); close();

View File

@ -3,7 +3,6 @@
#include"player.h" #include"player.h"
void Player::print(int cameraX){ void Player::print(int cameraX){
rect = {posX,posY,szW,szH};
SDL_Rect cameraFix = rect; SDL_Rect cameraFix = rect;
cameraFix.x -= cameraX; cameraFix.x -= cameraX;
@ -96,6 +95,8 @@ void Player::move(){
//Convert and set new int position //Convert and set new int position
posX += static_cast<int>(x); posX += static_cast<int>(x);
posY -= static_cast<int>(y+0.5); posY -= static_cast<int>(y+0.5);
rect = {posX,posY,szW,szH};
}; };
int Player::check(SDL_Rect rectA){ int Player::check(SDL_Rect rectA){