/* * KartFactory.cpp * * Created on: Aug 19, 2020 * Author: fredrick */ #include "KartFactory.h" namespace JamSpook { KartFactory::KartFactory(weak_ptr assetSystem, weak_ptr graphicsSystem, weak_ptr physicsSystem, weak_ptr interactionSystem, weak_ptr renderLayer): mAssetSystem(assetSystem), mGraphicsSystem(graphicsSystem), mPhysicsSystem(physicsSystem), mInteractionSystem(interactionSystem), mRenderLayer(renderLayer) {} KartFactory::~KartFactory() {} shared_ptr KartFactory::createKart(const vec3 position, const vec3 eulerRotationXYZDegrees) { // Create instance shared_ptr entity = make_shared(); entity->setEntityTag("playerkart"); SceneGraph::addEntity(entity); // Create transform mat4 rotation = eulerAngleXYZ( radians(static_cast(eulerRotationXYZDegrees.x)), radians(static_cast(eulerRotationXYZDegrees.y)), radians(static_cast(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 physicsComponent = make_shared( 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 = make_shared(transform, entity, getShared(mGraphicsSystem), mRenderLayer); entity->addComponent(graphicsComponent); // Add the ModelRenderable of a ball ModelRenderableFactory* modelFactory = new ModelRenderableFactory(mAssetSystem); shared_ptr modelRenderableKart = modelFactory->createModel("kart-vehicle.bcosm", "kart-vehicle-diffuse.png", "kart-vehicle-model"); graphicsComponent->addRenderable(modelRenderableKart); shared_ptr 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 = lightSourceFactory->createPointLight(vec3(1,1,1), 0.02f, 0.03f, transform); graphicsComponent->setLightSource(lightSource); delete lightSourceFactory; // Add interaction component shared_ptr interactionComponent = make_shared(transform, entity, getShared(mInteractionSystem)); entity->addComponent(interactionComponent); // Return instance return entity; } } // namespace JamSpook