JAMSPOOK-13 - Car and driver implemented!

- Implemented car and driver
- Tweaked the friction of the kart physics component to slide more easily.
This commit is contained in:
Fredrick Amnehagen 2020-08-19 16:48:19 +02:00
parent 9135e973b9
commit 4aed7f66e1
10 changed files with 439 additions and 20 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -265,20 +265,32 @@ void JamSpookGame::set()
goalFactory.reset(); goalFactory.reset();
// Create the ball // // Create the ball
unique_ptr<BallFactory> ballFactory = make_unique<BallFactory>(dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))), // unique_ptr<BallFactory> ballFactory = make_unique<BallFactory>(dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))),
graphicsSystem, // graphicsSystem,
dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))), // dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))),
renderLayerGame); // renderLayerGame);
//
// ballFactory->createBall(vec3(0, 3, 0), // position
// 1.0f, // size
// 10.0f, // size*2
// 0.1f, // restitution
// "base", // ballName
// vec3(1,1,1)); // lightColor
//
// ballFactory.reset();
ballFactory->createBall(vec3(0, 3, 0), // position // Create the player kart
1.0f, // size unique_ptr<KartFactory> kartFactory = make_unique<KartFactory>(
10.0f, // size*2 dynamic_pointer_cast<AssetSystem>(findSystem(IDCache::get("AssetSystem"))),
0.1f, // restitution graphicsSystem,
"base", // ballName dynamic_pointer_cast<PhysicsSystem>(findSystem(IDCache::get("PhysicsSystem"))),
vec3(1,1,1)); // lightColor renderLayerGame);
ballFactory.reset(); kartFactory->createKart(vec3(0, 3, 0), // position
vec3(0, 0, 0)); // euler degrees rotation, XYZ
kartFactory.reset();
// Set game state // Set game state
setGameState(ecos::core::GameState::InGame); setGameState(ecos::core::GameState::InGame);

View File

@ -12,10 +12,15 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <string> #include <string>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <ecos/utility/FileSystem.h>
#include <ecos/utility/Random.h>
#include <ecos/core/Log.h> #include <ecos/core/Log.h>
#include <ecos/core/Game.h> #include <ecos/core/Game.h>
#include <ecos/core/IDCache.h> #include <ecos/core/IDCache.h>
@ -25,14 +30,17 @@
#include <ecos/core/SysCall.h> #include <ecos/core/SysCall.h>
#include <ecos/core/BroadcastObservable.h> #include <ecos/core/BroadcastObservable.h>
#include <ecos/core/CycleRegulationSystem.h> #include <ecos/core/CycleRegulationSystem.h>
#include <ecos/utility/FileSystem.h>
#include <ecos/utility/Random.h>
#include <ecos/asset/AssetSystem.h> #include <ecos/asset/AssetSystem.h>
#include <ecos/asset/AssetFileSystem.h> #include <ecos/asset/AssetFileSystem.h>
#include <ecos/physics/PhysicsSystem.h> #include <ecos/physics/PhysicsSystem.h>
#include <ecos/physics/loaders/CollisionShapeLoader.h> #include <ecos/physics/loaders/CollisionShapeLoader.h>
#include <ecos/animation/AnimationSystem.h> #include <ecos/animation/AnimationSystem.h>
#include <ecos/interaction/InteractionSystem.h> #include <ecos/interaction/InteractionSystem.h>
#include <ecos/graphics/GraphicsSystem.h> #include <ecos/graphics/GraphicsSystem.h>
#include <ecos/graphics/RenderLayer.h> #include <ecos/graphics/RenderLayer.h>
#include <ecos/graphics/RenderLayerFactory.h> #include <ecos/graphics/RenderLayerFactory.h>
@ -48,6 +56,7 @@
#include <ecos/graphics/loaders/ShaderProgramLoader.h> #include <ecos/graphics/loaders/ShaderProgramLoader.h>
#include <ecos/graphics/loaders/TextureBlankLoader.h> #include <ecos/graphics/loaders/TextureBlankLoader.h>
#include <ecos/graphics/loaders/TextureFileLoader.h> #include <ecos/graphics/loaders/TextureFileLoader.h>
#include <ecos/sound/SoundSystem.h> #include <ecos/sound/SoundSystem.h>
#include <ecos/sound/loaders/SoundEffectLoader.h> #include <ecos/sound/loaders/SoundEffectLoader.h>
@ -56,6 +65,7 @@
#include "entities/GoalFactory.h" #include "entities/GoalFactory.h"
#include "entities/BallFactory.h" #include "entities/BallFactory.h"
#include "entities/BoxFactory.h" #include "entities/BoxFactory.h"
#include "entities/KartFactory.h"
namespace JamSpook { namespace JamSpook {
@ -69,6 +79,7 @@ using std::bind;
using std::vector; using std::vector;
using std::string; using std::string;
using std::to_string; using std::to_string;
using glm::ivec2; using glm::ivec2;
using glm::vec2; using glm::vec2;
using glm::vec3; using glm::vec3;
@ -77,6 +88,11 @@ using glm::mat4;
using glm::translate; using glm::translate;
using glm::normalize; using glm::normalize;
using ecos::utility::getWorkingPath;
using ecos::utility::getRealPath;
using ecos::utility::getFloatInRange;
using ecos::utility::getIntInRange;
using ecos::core::logging::Log; using ecos::core::logging::Log;
using ecos::core::logging::LogLevel; using ecos::core::logging::LogLevel;
using ecos::core::Game; using ecos::core::Game;
@ -87,19 +103,20 @@ using ecos::core::SysCall;
using ecos::core::BroadcastObservable; using ecos::core::BroadcastObservable;
using ecos::core::Window; using ecos::core::Window;
using ecos::core::CycleRegulationSystem; using ecos::core::CycleRegulationSystem;
using ecos::utility::getWorkingPath;
using ecos::utility::getRealPath;
using ecos::utility::getFloatInRange;
using ecos::utility::getIntInRange;
using ecos::asset::AssetSystem; using ecos::asset::AssetSystem;
using ecos::asset::AssetFileSystem; using ecos::asset::AssetFileSystem;
using ecos::asset::DataManagementMode;
using ecos::physics::PhysicsSystem; using ecos::physics::PhysicsSystem;
using ecos::physics::CollisionShapeLoader; using ecos::physics::CollisionShapeLoader;
using ecos::graphics::TextureBlankLoader;
using ecos::animation::AnimationSystem; using ecos::animation::AnimationSystem;
using ecos::interaction::InteractionSystem; using ecos::interaction::InteractionSystem;
using ecos::interaction::InputDeviceState; using ecos::interaction::InputDeviceState;
using ecos::interaction::ButtonState; using ecos::interaction::ButtonState;
using ecos::graphics::GraphicsSystem; using ecos::graphics::GraphicsSystem;
using ecos::graphics::RenderLayer; using ecos::graphics::RenderLayer;
using ecos::graphics::RenderLayerFactory; using ecos::graphics::RenderLayerFactory;
@ -119,10 +136,11 @@ using ecos::graphics::TextureBlankLoader;
using ecos::graphics::ShaderType; using ecos::graphics::ShaderType;
using ecos::graphics::ShaderAsset; using ecos::graphics::ShaderAsset;
using ecos::graphics::ShaderProgram; using ecos::graphics::ShaderProgram;
using ecos::asset::DataManagementMode;
using ecos::sound::SoundSystem; using ecos::sound::SoundSystem;
using ecos::sound::SoundEffectLoader; using ecos::sound::SoundEffectLoader;
///
class JamSpookGame: class JamSpookGame:
public Game public Game
{ {

View File

@ -72,6 +72,7 @@ using glm::eulerAngleXYZ;
using glm::radians; using glm::radians;
using ecos::utility::getShared; using ecos::utility::getShared;
using ecos::core::IDCache; using ecos::core::IDCache;
using ecos::core::logging::Log; using ecos::core::logging::Log;
using ecos::core::logging::LogLevel; using ecos::core::logging::LogLevel;
@ -80,8 +81,10 @@ using ecos::core::Message;
using ecos::core::Entity; using ecos::core::Entity;
using ecos::core::SceneGraph; using ecos::core::SceneGraph;
using ecos::core::ICCResponseComponent; using ecos::core::ICCResponseComponent;
using ecos::asset::AssetSystem; using ecos::asset::AssetSystem;
using ecos::asset::DataManagementMode; using ecos::asset::DataManagementMode;
using ecos::graphics::GraphicsSystem; using ecos::graphics::GraphicsSystem;
using ecos::graphics::GraphicsComponent; using ecos::graphics::GraphicsComponent;
using ecos::graphics::RenderLayer; using ecos::graphics::RenderLayer;
@ -97,6 +100,7 @@ using ecos::graphics::Mesh;
using ecos::graphics::MeshFactory; using ecos::graphics::MeshFactory;
using ecos::graphics::ModelRenderableFactory; using ecos::graphics::ModelRenderableFactory;
using ecos::graphics::ModelRenderable; using ecos::graphics::ModelRenderable;
using ecos::physics::PhysicsSystem; using ecos::physics::PhysicsSystem;
using ecos::physics::PhysicsComponent; using ecos::physics::PhysicsComponent;
using ecos::physics::ColliderFactory; using ecos::physics::ColliderFactory;

View File

@ -0,0 +1,105 @@
/*
* KartFactory.cpp
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#include "KartFactory.h"
namespace JamSpook {
KartFactory::KartFactory(weak_ptr<AssetSystem> assetSystem,
weak_ptr<GraphicsSystem> graphicsSystem,
weak_ptr<PhysicsSystem> physicsSystem,
weak_ptr<RenderLayer> renderLayer):
mAssetSystem(assetSystem),
mGraphicsSystem(graphicsSystem),
mPhysicsSystem(physicsSystem),
mRenderLayer(renderLayer)
{}
KartFactory::~KartFactory()
{}
shared_ptr<Entity> KartFactory::createKart(const vec3 position,
const vec3 eulerRotationXYZDegrees)
{
// Create instance
shared_ptr<Entity> entity = make_shared<Entity>();
entity->setEntityTag("playerkart");
SceneGraph::addEntity(entity);
// Create transform
mat4 rotation = eulerAngleXYZ(
radians(static_cast<float>(eulerRotationXYZDegrees.x)),
radians(static_cast<float>(eulerRotationXYZDegrees.y)),
radians(static_cast<float>(eulerRotationXYZDegrees.z)));
mat4 translation = translate(mat4(1.0f), position);
mat4 transform = rotation * translation;// * rotateToCenter;
// Add physics component
ColliderFactory* colliderFactory = new ColliderFactory(mPhysicsSystem, mAssetSystem);
shared_ptr<KartPhysicsComponent> physicsComponent =
make_shared<KartPhysicsComponent>(
transform,
entity,
getShared(mPhysicsSystem),
getShared(mPhysicsSystem),
getShared(mPhysicsSystem),
colliderFactory->createConvexHullCollider(position,
quat_cast(rotation),
"kart-hulls.bcosp", // filePath
"kart-hulls-shape", // name
100.0f, // mass
entity,
entity->getEntityTag()));
delete colliderFactory;
// Note: Editing of rigid body params, maybe only has effect is it has been added to the world.
physicsComponent->getCollider()->getRigidBody()->setLinearFactor(btVector3(1,1,1)); // allow all positional movement movement
physicsComponent->getCollider()->getRigidBody()->setAngularFactor(btVector3(1,1,1)); // allow all rotational movement movement
physicsComponent->getCollider()->getRigidBody()->setRestitution(0.1f);
physicsComponent->getCollider()->getRigidBody()->setFriction(0.01f);
// physicsComponent->getCollider()->getRigidBody()->setGravity(btVector3(0, 0, 0)); // no gravity, length of zero
physicsComponent->getCollider()->getRigidBody()->setActivationState(DISABLE_DEACTIVATION); // RigidBody may never sleep. (Note: Or else it will not respond to setLinearVelocity when standing still for too long(a couple of seconds).)
entity->addComponent(physicsComponent);
// Add graphics component
shared_ptr<GraphicsComponent> graphicsComponent = make_shared<GraphicsComponent>(transform,
entity,
getShared(mGraphicsSystem),
mRenderLayer);
entity->addComponent(graphicsComponent);
// Add the ModelRenderable of a ball
ModelRenderableFactory* modelFactory = new ModelRenderableFactory(mAssetSystem);
shared_ptr<ModelRenderable> modelRenderableKart = modelFactory->createModel("kart-vehicle.bcosm",
"kart-vehicle-diffuse.png",
"kart-vehicle-model");
graphicsComponent->addRenderable(modelRenderableKart);
shared_ptr<ModelRenderable> modelRenderableDriver = modelFactory->createModel("kart-driver.bcosm",
"kart-driver-diffuse.png",
"kart-driver-model");
graphicsComponent->addRenderable(modelRenderableDriver);
delete modelFactory;
// Add internal pointlight
LightSourceFactory* lightSourceFactory = new LightSourceFactory(mAssetSystem, mGraphicsSystem);
shared_ptr<LightSource> lightSource = lightSourceFactory->createPointLight(vec3(1,1,1),
0.02f,
0.03f,
transform);
graphicsComponent->setLightSource(lightSource);
delete lightSourceFactory;
// Return instance
return entity;
}
} // namespace JamSpook

141
src/entities/KartFactory.h Normal file
View File

@ -0,0 +1,141 @@
/*
* KartFactory.h
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#ifndef KARTFACTORY_H_
#define KARTFACTORY_H_
#include <cstdint>
#include <string>
#include <memory>
#include <functional>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/string_cast.hpp>
#include <ecos/utility/Memory.h>
#include <ecos/core/IDCache.h>
#include <ecos/core/Log.h>
#include <ecos/core/BroadcastObservable.h>
#include <ecos/core/Message.h>
#include <ecos/core/Entity.h>
#include <ecos/core/SceneGraph.h>
#include <ecos/core/ICCResponseComponent.h>
#include <ecos/asset/AssetSystem.h>
#include <ecos/asset/DataManagementMode.h>
#include <ecos/physics/PhysicsSystem.h>
#include <ecos/physics/PhysicsComponent.h>
#include <ecos/physics/ColliderFactory.h>
#include <ecos/physics/Collider.h>
#include <ecos/graphics/GraphicsSystem.h>
#include <ecos/graphics/GraphicsComponent.h>
#include <ecos/graphics/RenderLayer.h>
#include <ecos/graphics/TextureFactory.h>
#include <ecos/graphics/Texture.h>
#include <ecos/graphics/assets/ShaderAsset.h>
#include <ecos/graphics/ShaderProgram.h>
#include <ecos/graphics/ShaderProgramFactory.h>
#include <ecos/graphics/Material.h>
#include <ecos/graphics/MaterialFactory.h>
#include <ecos/graphics/Mesh.h>
#include <ecos/graphics/MeshFactory.h>
#include <ecos/graphics/ModelRenderable.h>
#include <ecos/graphics/lighting/LightSource.h>
#include <ecos/graphics/lighting/LightSourceFactory.h>
#include <ecos/graphics/ModelRenderableFactory.h>
#include <ecos/graphics/ShaderType.h>
#include "KartPhysicsComponent.h"
namespace JamSpook {
using std::string;
using std::shared_ptr;
using std::make_shared;
using std::weak_ptr;
using std::dynamic_pointer_cast;
using std::function;
using std::bind;
using std::vector;
using glm::vec3;
using glm::mat4;
using glm::quat;
using glm::quat_cast;
using glm::translate;
using glm::scale;
using glm::rotate;
using glm::eulerAngleXYZ;
using glm::radians;
using ecos::utility::getShared;
using ecos::core::IDCache;
using ecos::core::logging::Log;
using ecos::core::logging::LogLevel;
using ecos::core::BroadcastObservable;
using ecos::core::Message;
using ecos::core::Entity;
using ecos::core::SceneGraph;
using ecos::core::ICCResponseComponent;
using ecos::asset::AssetSystem;
using ecos::asset::DataManagementMode;
using ecos::graphics::GraphicsSystem;
using ecos::graphics::GraphicsComponent;
using ecos::graphics::RenderLayer;
using ecos::graphics::TextureFactory;
using ecos::graphics::Texture;
using ecos::graphics::ShaderType;
using ecos::graphics::ShaderAsset;
using ecos::graphics::ShaderProgram;
using ecos::graphics::ShaderProgramFactory;
using ecos::graphics::Material;
using ecos::graphics::MaterialFactory;
using ecos::graphics::Mesh;
using ecos::graphics::MeshFactory;
using ecos::graphics::ModelRenderableFactory;
using ecos::graphics::ModelRenderable;
using ecos::graphics::lighting::LightSource;
using ecos::graphics::lighting::LightSourceFactory;
using ecos::physics::PhysicsSystem;
using ecos::physics::PhysicsComponent;
using ecos::physics::ColliderFactory;
using ecos::physics::Collider;
/// Factory to simplify player kart entity creation
class KartFactory
{
private:
weak_ptr<AssetSystem> mAssetSystem;
weak_ptr<GraphicsSystem> mGraphicsSystem;
weak_ptr<PhysicsSystem> mPhysicsSystem;
weak_ptr<RenderLayer> mRenderLayer;
public:
KartFactory(weak_ptr<AssetSystem> assetSystem,
weak_ptr<GraphicsSystem> graphicsSystem,
weak_ptr<PhysicsSystem> physicsSystem,
weak_ptr<RenderLayer> renderLayer);
virtual ~KartFactory();
/// Compose a kart entity
shared_ptr<Entity> createKart(const vec3 position,
const vec3 eulerRotationXYZDegrees);
};
} // namespace JamSpook
#endif // KARTFACTORY_H_

View File

@ -0,0 +1,57 @@
/*
* KartPhysicsComponent.cpp
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#include "KartPhysicsComponent.h"
namespace JamSpook {
KartPhysicsComponent::KartPhysicsComponent(mat4 transform,
shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity,
weak_ptr<BroadcastObservable<const milliseconds> > physicsSystem,
weak_ptr<BroadcastObservable<shared_ptr<CollisionStateChangeMessage> > > physicsCollisionSubSystem,
weak_ptr<BroadcastObservable<shared_ptr<ColliderQueryMessage> > > physicsColliderQuerySubSystem,
shared_ptr<Collider> collider):
PhysicsComponent(transform,
entity,
physicsSystem,
physicsCollisionSubSystem,
physicsColliderQuerySubSystem,
collider)
{}
KartPhysicsComponent::~KartPhysicsComponent()
{}
void KartPhysicsComponent::update(const milliseconds dtms)
{
// Log::write(LogLevel::trace, "CollectablePhysicsComponent - update");
PhysicsComponent::update(dtms);
if (getPosition().y < -100.0f)
{
setPosition(vec3(getFloatInRange(-10.0f, 10.0f),
10, /// above the ball
getFloatInRange(-10.0f, 10.0f)));
}
}
void KartPhysicsComponent::onICCMessage(shared_ptr<Message> message)
{
PhysicsComponent::onICCMessage(message);
}
void KartPhysicsComponent::onCollision(const string& tag)
{
if (tag == "ground-plane")
{
vec3 position = getPosition();
setPosition(vec3(position.x, 10.0f, position.z));
ICCBroadcast(make_shared<Message>(IDCache::get("GroundContactMessage")));
}
}
} // namespace JamSpook

View File

@ -0,0 +1,82 @@
/*
* KartPhysicsComponent.h
*
* Created on: Aug 19, 2020
* Author: fredrick
*/
#ifndef KARTPHYSICSCOMPONENT_H_
#define KARTPHYSICSCOMPONENT_H_
#include <chrono>
#include <memory>
#include <cstdint>
#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <ecos/utility/Random.h>
#include <ecos/core/IDCache.h>
#include <ecos/core/Log.h>
#include <ecos/core/Message.h>
#include <ecos/core/BroadcastObservable.h>
#include <ecos/physics/PhysicsSystem.h>
#include <ecos/physics/TransformChangeMessage.h>
#include <ecos/physics/CollisionStateChangeMessage.h>
#include <ecos/physics/ColliderQueryMessage.h>
#include <ecos/physics/PhysicsComponent.h>
#include <ecos/physics/Collider.h>
namespace JamSpook {
using std::chrono::milliseconds;
using std::weak_ptr;
using std::shared_ptr;
using std::make_shared;
using std::dynamic_pointer_cast;
using std::string;
using glm::vec3;
using glm::mat4;
using glm::translate;
using glm::scale;
using ecos::utility::getFloatInRange;
using ecos::core::IDCache;
using ecos::core::logging::Log;
using ecos::core::logging::LogLevel;
using ecos::core::Message;
using ecos::core::BroadcastObservable;
using ecos::physics::PhysicsSystem;
using ecos::physics::PhysicsComponent;
using ecos::physics::Collider;
using ecos::physics::TransformChangeMessage;
using ecos::physics::CollisionStateChangeMessage;
using ecos::physics::ColliderQueryMessage;
///
class KartPhysicsComponent:
public ecos::physics::PhysicsComponent
{
public:
KartPhysicsComponent(mat4 transform,
shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity,
weak_ptr<BroadcastObservable<const milliseconds> > physicsSystem,
weak_ptr<BroadcastObservable<shared_ptr<CollisionStateChangeMessage> > > physicsCollisionSubSystem,
weak_ptr<BroadcastObservable<shared_ptr<ColliderQueryMessage> > > physicsColliderQuerySubSystem,
shared_ptr<Collider> collider);
virtual ~KartPhysicsComponent();
virtual void update(const milliseconds dtms);
virtual void onICCMessage(shared_ptr<Message> message);
virtual void onCollision(const string& tag);
};
} // namespace JamSpook
#endif // KARTPHYSICSCOMPONENT_H_