Added physics-related functions to Entity and mouse clicks vs holds

This commit is contained in:
grayson committed 2021-11-02 01:41:53 -05:00
1 parent 43c3d3930f
commit 3e403681a3
12 files changed
+174 -46

No files matched your search

+45 -7
View File
@@ -159,16 +159,24 @@ namespace nf {
}
bool Application::isKeyPressed(unsigned int code) {
if (code > 7 && code < 164) {
if (m_keysPressed[code]) {
m_keysPressed[code] = false;
if (code > 7 && code < 164)
if (m_inputPressed[code]) {
m_inputPressed[code] = false;
return true;
}
}
return false;
}
bool Application::isMouse(unsigned int code) {
bool Application::isMouseClicked(unsigned int code) {
if (code < 7)
if (m_inputPressed[code]) {
m_inputPressed[code] = false;
return true;
}
return false;
}
bool Application::isMouseHeld(unsigned int code) {
if (code < 7 && GetForegroundWindow() == m_window)
return GetKeyState(code) & 0x8000;
return false;
@@ -385,12 +393,42 @@ namespace nf {
}
case WM_KEYDOWN: {
if (wParam < 164 && !(lParam & (1 << 30)) && GetFocus() == hWnd)
app->m_keysPressed[wParam] = true;
app->m_inputPressed[wParam] = true;
return 0;
}
case WM_KEYUP: {
if (wParam < 164 && GetFocus() == hWnd)
app->m_keysPressed[wParam] = false;
app->m_inputPressed[wParam] = false;
return 0;
}
case WM_LBUTTONDOWN: {
if (GetFocus() == hWnd)
app->m_inputPressed[1] = true;
return 0;
}
case WM_LBUTTONUP: {
if (GetFocus() == hWnd)
app->m_inputPressed[1] = false;
return 0;
}
case WM_RBUTTONDOWN: {
if (GetFocus() == hWnd)
app->m_inputPressed[2] = true;
return 0;
}
case WM_RBUTTONUP: {
if (GetFocus() == hWnd)
app->m_inputPressed[2] = false;
return 0;
}
case WM_MBUTTONDOWN: {
if (GetFocus() == hWnd)
app->m_inputPressed[4] = true;
return 0;
}
case WM_MBUTTONUP: {
if (GetFocus() == hWnd)
app->m_inputPressed[4] = false;
return 0;
}
case WM_SETCURSOR: {
+13 -5
View File
@@ -30,10 +30,6 @@ namespace nf {
m_running = true;
}
void Gamestate::onEnter() {
}
bool Gamestate::isRunning() {
return m_running;
}
@@ -42,16 +38,28 @@ namespace nf {
return m_loading;
}
void Gamestate::onEnter() {
}
void Gamestate::update(float deltaTime) {
}
void Gamestate::render(Renderer& renderer) {
}
Camera* Gamestate::getCamera() {
return camera;
}
void Gamestate::render(Renderer& renderer) {
void Gamestate::setGravity(const Vec3& gravity) {
app->getPhysicsEngine()->setGravity(gravity * 9.81f);
}
void Gamestate::setGravity(float strength) {
app->getPhysicsEngine()->setGravity(Vec3(0.0f, -9.81f * strength, 0.0f));
}
void Gamestate::onExit() {
+1 -1
View File
@@ -84,7 +84,7 @@ namespace nf {
m_triggered = false;
if (mousePos.x >= posX && mousePos.x <= posX + width && mousePos.y <= windowHeight - posY && mousePos.y >= (windowHeight - posY) - height) {
m_currentTexture = m_hoverTexture;
if (app->isMouse(NFI_LEFTMOUSE))
if (app->isMouseHeld(NFI_LEFTMOUSE))
m_clicked = true;
else if (m_clicked) {
m_triggered = true;
+12
View File
@@ -102,6 +102,18 @@ namespace nf {
m_update = true;
}
void Entity::setVelocity(float x, float y, float z) {
setVelocity(Vec3(x, y, z));
}
void Entity::setVelocity(const Vec3& velocity) {
Application::getApp()->getPhysicsEngine()->setActorVelocity(this, velocity);
}
void Entity::setMass(float mass) {
Application::getApp()->getPhysicsEngine()->setActorMass(this, mass);
}
bool Entity::needsPhysicsUpdate() {
if (m_update) {
m_update = false;
+47 -17
View File
@@ -3,7 +3,6 @@
#include "Application.h"
#include "Entity.h"
#include "Model.h"
#include "Utility.h"
//Remove this
#include "Input.h"
@@ -79,6 +78,52 @@ namespace nf {
m_scene->addActor(*ground);
}
void PhysicsEngine::setGravity(const Vec3& grav) {
if (m_scene) {
m_scene->setGravity(PxVec3(grav.x, grav.y, grav.z));
unsigned int count = m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC);
PxActor** actors = new PxActor * [count];
m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC, actors, count);
for (unsigned int i = 0; i < count; i++) {
if (((PxRigidDynamic*)actors[i])->isSleeping())
((PxRigidDynamic*)actors[i])->wakeUp();
}
delete[] actors;
}
}
void PhysicsEngine::setActorVelocity(Entity* ent, const Vec3& vel) {
unsigned int count = m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC);
PxActor** actors = new PxActor * [count];
m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC, actors, count);
for (unsigned int i = 0; i < count; i++) {
Entity* curr = (Entity*)actors[i]->userData;
if (curr == ent) {
((PxRigidDynamic*)actors[i])->setLinearVelocity(PxVec3(vel.x, vel.y, vel.z), true);
break;
}
}
delete[] actors;
}
void PhysicsEngine::setActorMass(Entity* ent, float mass) {
unsigned int count = m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC);
PxActor** actors = new PxActor * [count];
m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC, actors, count);
for (unsigned int i = 0; i < count; i++) {
Entity* curr = (Entity*)actors[i]->userData;
if (curr == ent) {
((PxRigidDynamic*)actors[i])->setMass(mass);
break;
}
}
delete[] actors;
}
void PhysicsEngine::update(float dt) {
if (!m_scene || !m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC | PxActorTypeFlag::eRIGID_STATIC)) return;
@@ -86,21 +131,7 @@ namespace nf {
PxActor** actors = new PxActor*[count];
m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC | PxActorTypeFlag::eRIGID_STATIC, actors, count);
if (m_app->isMouse(NFI_LEFTMOUSE)) {
Vec3 pos = m_app->getCurrentState()->getCamera()->getPosition();
PxVec3 camPos(pos.x, pos.y, pos.z);
PxQuat q(PxIdentity);
((PxRigidDynamic*)actors[1])->setGlobalPose(PxTransform(camPos, q));
Vec3 camDir = m_app->getCurrentState()->getCamera()->getRotation();
float speed = 100.0f;
PxRigidBodyExt::updateMassAndInertia(*(PxRigidDynamic*)actors[1], 1000.0);
((PxRigidDynamic*)actors[1])->setAngularVelocity(PxVec3(0.0));
((PxRigidDynamic*)actors[1])->setLinearVelocity(PxVec3(camDir.x * speed, camDir.y * speed, camDir.z * speed));
}
//TODO: CHANGE THIS 1 TO A 0!!!!
//Starting at 1 since 0 is the ground plane
for (unsigned int i = 1; i < count; i++) {
Entity* currEnt = (Entity*)actors[i]->userData;
if (!currEnt->needsPhysicsUpdate()) continue;
@@ -193,7 +224,6 @@ namespace nf {
else
it++;
}
m_scene->release();
m_scene = nullptr;
}
+1 -1
View File
@@ -157,7 +157,7 @@ namespace nf {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 10000.0f);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 1000.0f);
camera->bind(m_gBufferShader, m_lightingShader, m_cubemapShader);
//First, fill the gBuffer with entities
+3 -2
View File
@@ -40,7 +40,8 @@ namespace nf {
int getFPS() const;
bool isKeyHeld(unsigned int code);
bool isKeyPressed(unsigned int code);
bool isMouse(unsigned int code);
bool isMouseClicked(unsigned int code);
bool isMouseHeld(unsigned int code);
void trackMouse(bool track);
void getMouseDiff(int& x, int& y);
Vec2 getMousePos();
@@ -90,7 +91,7 @@ namespace nf {
bool m_stateChangeStarted;
//Array of booleans that represent keyboard and mouse input minus the scrollwheel
std::array<bool, 164> m_keysPressed;
std::array<bool, 164> m_inputPressed;
unsigned int m_mouseX, m_mouseY;
bool m_trackingMouse;
bool m_mouseTrackFirst;
+9 -4
View File
@@ -27,19 +27,24 @@ namespace nf {
void setPosition(float x, float y, float z);
void setPosition(const Vec3& position);
void setPositionPhysics(const Vec3& position);
void setRotation(float x, float y, float z);
void setRotation(const Vec3& rotation);
void setRotationPhysics(const Vec4& rotation);
void setScale(float x);
void setScale(float x, float y, float z);
void setScale(const Vec3& scale);
void setScale(float x);
bool needsPhysicsUpdate();
void setVelocity(float x, float y, float z);
void setVelocity(const Vec3& velocity);
void setMass(float mass);
const Vec3& getPosition();
const Vec4& getRotation();
const Vec3& getScale();
void setPositionPhysics(const Vec3& position);
void setRotationPhysics(const Vec4& rotation);
bool needsPhysicsUpdate();
void render(Shader* shader, bool onlyDepth);
Model* getModel() const;
#ifdef NFENGINE
+9 -4
View File
@@ -16,18 +16,23 @@ namespace nf {
class Gamestate {
public:
Gamestate();
//TODO: Add this to other objects
Gamestate(const Gamestate& other) = delete;
virtual void onEnter();
void run(Application* app, bool physics = true);
bool isRunning();
bool isLoading();
void run(Application* app, bool physics = true);
virtual void onEnter();
virtual void update(float deltaTime);
Camera* getCamera();
virtual void render(Renderer& renderer);
Camera* getCamera();
//In units of Earth gravity (9.81 m/s^2)
void setGravity(const Vec3& gravity);
void setGravity(float strength);
virtual void onExit();
void stop();
+5
View File
@@ -3,6 +3,8 @@
#include <PxConfig.h>
#include <PxPhysicsAPI.h>
#include "Utility.h"
using namespace physx;
namespace nf {
@@ -15,6 +17,9 @@ namespace nf {
PhysicsEngine(Application* app);
void newScene();
void setGravity(const Vec3& grav);
void setActorVelocity(Entity* ent, const Vec3& vel);
void setActorMass(Entity* ent, float mass);
void update(float dt);
void addMesh(Model* model, std::vector<float>& vertices);
void addActor(Entity* entity);