up down is broken
This commit is contained in:
+7
-2
@@ -1,16 +1,21 @@
|
|||||||
#ifndef DELTA_ENTITY
|
#ifndef DELTA_ENTITY
|
||||||
#define DELTA_ENTITY
|
#define DELTA_ENTITY
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_main.h>
|
||||||
|
|
||||||
#include "renderer/camera.hpp"
|
#include "renderer/camera.hpp"
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
|
|
||||||
class DeltaEntity {
|
class DeltaEntity {
|
||||||
public:
|
public:
|
||||||
DeltaVector3 pos;
|
DeltaVector3 pos;
|
||||||
|
DeltaVector3 velocity;
|
||||||
DeltaVector2 angle;
|
DeltaVector2 angle;
|
||||||
|
|
||||||
DeltaEntity(float x, float y, float z);
|
DeltaEntity() {pos.x = 0; pos.y = 0; pos.z = 0; velocity = DeltaVector3(); angle = DeltaVector2();};
|
||||||
void draw(DeltaCamera camera);
|
DeltaEntity(float x, float y, float z) {pos.x = x; pos.y = y; pos.z = z; velocity = DeltaVector3(); angle = DeltaVector2(); };
|
||||||
|
void draw(SDL_Renderer *renderer);
|
||||||
void update(float deltaTime);
|
void update(float deltaTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,11 @@ class DeltaVector3 {
|
|||||||
float x, y, z;
|
float x, y, z;
|
||||||
DeltaVector3(float xval, float yval, float zval);
|
DeltaVector3(float xval, float yval, float zval);
|
||||||
DeltaVector3();
|
DeltaVector3();
|
||||||
|
DeltaVector3 operator+(const DeltaVector3& other) const;
|
||||||
|
DeltaVector3 operator-(const DeltaVector3& other) const;
|
||||||
|
DeltaVector3 operator*(float a) const;
|
||||||
|
DeltaVector3& operator+=(const DeltaVector3& other);
|
||||||
|
DeltaVector3& operator-=(const DeltaVector3& other);
|
||||||
float distanceToVector(const DeltaVector3 &vec) const;
|
float distanceToVector(const DeltaVector3 &vec) const;
|
||||||
float distanceToPos(float x, float y) const;
|
float distanceToPos(float x, float y) const;
|
||||||
float dotProduct(const DeltaVector3 &vec) const;
|
float dotProduct(const DeltaVector3 &vec) const;
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef DELTA_PLAYER
|
||||||
|
#define DELTA_PLAYER
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_main.h>
|
||||||
|
|
||||||
|
#include "renderer/camera.hpp"
|
||||||
|
#include "entity.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
|
|
||||||
|
class Player: public DeltaEntity {
|
||||||
|
public:
|
||||||
|
DeltaCamera camera;
|
||||||
|
DeltaVector2 direction;
|
||||||
|
|
||||||
|
Player() {pos.x = 0; pos.y = 0; pos.z = 0; velocity = DeltaVector3(); angle = DeltaVector2(); camera = DeltaCamera(0, 0, 0);};
|
||||||
|
Player(float x, float y, float z) {pos.x = x; pos.y = y; pos.z = z; velocity = DeltaVector3(); angle = DeltaVector2(); camera = DeltaCamera(0,0,0);};
|
||||||
|
|
||||||
|
void draw(SDL_Renderer *renderer);
|
||||||
|
void update(float deltaTime);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,6 +8,7 @@ class DeltaCamera {
|
|||||||
DeltaVector3 pos;
|
DeltaVector3 pos;
|
||||||
DeltaVector2 angle;
|
DeltaVector2 angle;
|
||||||
|
|
||||||
|
DeltaCamera() {pos = DeltaVector3(); angle = DeltaVector2();};
|
||||||
DeltaCamera(float x, float y, float z);
|
DeltaCamera(float x, float y, float z);
|
||||||
DeltaVector2 projectPointVec(const DeltaVector3 &worldPos);
|
DeltaVector2 projectPointVec(const DeltaVector3 &worldPos);
|
||||||
DeltaVector2 projectPoint(float worldX, float worldY, float worldZ);
|
DeltaVector2 projectPoint(float worldX, float worldY, float worldZ);
|
||||||
|
|||||||
+31
-2
@@ -3,12 +3,14 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_main.h>
|
#include <SDL3/SDL_main.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <string>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
#include "renderer/camera.hpp"
|
#include "renderer/camera.hpp"
|
||||||
#include "renderer/calls.hpp"
|
#include "renderer/calls.hpp"
|
||||||
|
#include "player.hpp"
|
||||||
|
|
||||||
static SDL_Window *window = NULL;
|
static SDL_Window *window = NULL;
|
||||||
static SDL_Renderer *renderer = NULL;
|
static SDL_Renderer *renderer = NULL;
|
||||||
@@ -16,6 +18,10 @@ static SDL_Renderer *renderer = NULL;
|
|||||||
DeltaVector2 temp1(125, 150), temp2(150, 120), temp3(100, 100);
|
DeltaVector2 temp1(125, 150), temp2(150, 120), temp3(100, 100);
|
||||||
DeltaVector2 topleft, bottomright, p;
|
DeltaVector2 topleft, bottomright, p;
|
||||||
|
|
||||||
|
Player player = Player(70, 110, 20);
|
||||||
|
|
||||||
|
double prev = 0, now = 0, deltaTime;
|
||||||
|
|
||||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
SDL_SetAppMetadata("delta engine game", "0.0", "com.miletici.game");
|
SDL_SetAppMetadata("delta engine game", "0.0", "com.miletici.game");
|
||||||
@@ -39,21 +45,44 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
|||||||
if (event->type == SDL_EVENT_QUIT) {
|
if (event->type == SDL_EVENT_QUIT) {
|
||||||
return SDL_APP_SUCCESS;
|
return SDL_APP_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (event->type == SDL_EVENT_KEY_DOWN) {
|
||||||
|
if (event->key.scancode == SDL_SCANCODE_ESCAPE) return SDL_APP_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||||
{
|
{
|
||||||
const double now = ((double)SDL_GetTicks()) / 1000.0;
|
now = ((double)SDL_GetTicks()) / 1000.0;
|
||||||
|
deltaTime = now - prev;
|
||||||
|
|
||||||
|
SDL_SetWindowTitle(window, std::to_string((int)(1 / deltaTime)).c_str());
|
||||||
|
|
||||||
|
player.update(deltaTime);
|
||||||
|
|
||||||
|
// drawing starts here
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 0, 255, SDL_ALPHA_OPAQUE);
|
SDL_SetRenderDrawColor(renderer, 255, 0, 255, SDL_ALPHA_OPAQUE);
|
||||||
rasterizeTriangle(renderer, temp1, temp2, temp3);
|
// rasterizeTriangle(renderer, temp1, temp2, temp3);
|
||||||
|
|
||||||
|
DeltaVector2 rendered = player.camera.projectPointVec({ 40, 10, 30 });
|
||||||
|
SDL_RenderPoint(renderer, rendered.x, rendered.y);
|
||||||
|
rendered = player.camera.projectPointVec({ 40, 210, 30 });
|
||||||
|
SDL_RenderPoint(renderer, rendered.x, rendered.y);
|
||||||
|
rendered = player.camera.projectPointVec({ 40, 10, -170 });
|
||||||
|
SDL_RenderPoint(renderer, rendered.x, rendered.y);
|
||||||
|
rendered = player.camera.projectPointVec({ 40, 210, -170 });
|
||||||
|
SDL_RenderPoint(renderer, rendered.x, rendered.y);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
|
prev = now;
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,33 @@ DeltaVector3::DeltaVector3(float xval, float yval, float zval) {
|
|||||||
z = zval;
|
z = zval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DeltaVector3 DeltaVector3::operator+(const DeltaVector3& other) const {
|
||||||
|
return DeltaVector3(x + other.x, y + other.y, z + other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeltaVector3 DeltaVector3::operator-(const DeltaVector3& other) const {
|
||||||
|
return DeltaVector3(x - other.x, y - other.y, z - other.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeltaVector3 DeltaVector3::operator*(float a) const {
|
||||||
|
return DeltaVector3(x * a, y * a, z * a);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeltaVector3& DeltaVector3::operator+=(const DeltaVector3& other) {
|
||||||
|
x += other.x;
|
||||||
|
y += other.y;
|
||||||
|
z += other.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
DeltaVector3& DeltaVector3::operator-=(const DeltaVector3& other) {
|
||||||
|
x -= other.x;
|
||||||
|
y -= other.y;
|
||||||
|
z -= other.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
float DeltaVector3::length() const {
|
float DeltaVector3::length() const {
|
||||||
return std::sqrt(x * x + y * y + z * z);
|
return std::sqrt(x * x + y * y + z * z);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_main.h>
|
||||||
|
#include <cmath>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "player.hpp"
|
||||||
|
|
||||||
|
void Player::draw(SDL_Renderer *renderer) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::update(float deltaTime) {
|
||||||
|
const bool *key_states = SDL_GetKeyboardState(NULL);
|
||||||
|
direction = {(float)(key_states[SDL_SCANCODE_D] - key_states[SDL_SCANCODE_A]), (float)(key_states[SDL_SCANCODE_W] - key_states[SDL_SCANCODE_S])};
|
||||||
|
|
||||||
|
DeltaVector2 angleChange;
|
||||||
|
angleChange.x = (float)(key_states[SDL_SCANCODE_RIGHT] - key_states[SDL_SCANCODE_LEFT]);
|
||||||
|
angleChange.y = (float)(key_states[SDL_SCANCODE_UP] - key_states[SDL_SCANCODE_DOWN]);
|
||||||
|
|
||||||
|
float dx = std::sin(angle.x), dy = std::cos(angle.x);
|
||||||
|
|
||||||
|
// if (key_states[SDL_SCANCODE_W])
|
||||||
|
velocity = DeltaVector3();
|
||||||
|
if (direction.y == 1) { velocity.x += dx * 200; velocity.y += dy * 200; }
|
||||||
|
if (direction.y == -1) { velocity.x -= dx * 200; velocity.y -= dy * 200; }
|
||||||
|
|
||||||
|
if (direction.x == 1) { velocity.x += dy * 200; velocity.y -= dx * 200; }
|
||||||
|
if (direction.x == -1) { velocity.x -= dy * 200; velocity.y += dx * 200; }
|
||||||
|
|
||||||
|
if (key_states[SDL_SCANCODE_SPACE]) {
|
||||||
|
velocity.z = 200;
|
||||||
|
}
|
||||||
|
if (key_states[SDL_SCANCODE_LSHIFT]) {
|
||||||
|
velocity.z = -200;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Log(std::to_string(pos.x).c_str());
|
||||||
|
SDL_Log(std::to_string(pos.y).c_str());
|
||||||
|
SDL_Log(std::to_string(pos.z).c_str());
|
||||||
|
|
||||||
|
angle.x += angleChange.x * 3.14 * deltaTime;
|
||||||
|
angle.y += angleChange.y * 3.14 * deltaTime;
|
||||||
|
|
||||||
|
pos += velocity * deltaTime;
|
||||||
|
|
||||||
|
camera.pos = pos;
|
||||||
|
camera.angle = angle;
|
||||||
|
}
|
||||||
@@ -33,12 +33,14 @@ DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) {
|
|||||||
|
|
||||||
// pitch translate
|
// pitch translate
|
||||||
|
|
||||||
calculatedPos.y = calculatedPos.y * cosineY - calculatedPos.z * sineY;
|
float tempY = calculatedPos.y, tempZ = calculatedPos.z;
|
||||||
calculatedPos.z = calculatedPos.y * sineY + calculatedPos.z * cosineY;
|
|
||||||
|
calculatedPos.y = tempY * cosineY - tempZ * sineY;
|
||||||
|
calculatedPos.z = tempY * sineY + tempZ * cosineY;
|
||||||
// dont touch calculatedPos.x
|
// dont touch calculatedPos.x
|
||||||
|
|
||||||
projected.x = calculatedPos.x * FOCAL / calculatedPos.y + SIMULATED_WIDTH / 2;
|
projected.x = calculatedPos.x * FOCAL / calculatedPos.y + SIMULATED_WIDTH / 2;
|
||||||
projected.y = calculatedPos.y * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2;
|
projected.y = calculatedPos.z * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2;
|
||||||
|
|
||||||
// now gotta convert y to our y
|
// now gotta convert y to our y
|
||||||
projected.y = SIMULATED_HEIGHT - projected.y;
|
projected.y = SIMULATED_HEIGHT - projected.y;
|
||||||
|
|||||||
Reference in New Issue
Block a user