Browse Source

Added track, fixed terrain

* Added racetrack and put an objective on it
* Fixing objective scaling so it's not confusing when I have to go in
and add/change them
* Terrain can now be generated at arbitrary positions. It's very leaky
right now memory-wise
* Updated Tracy to not crash
ogre-port2
Macoy Madson 6 months ago
parent
commit
37b57bd494
9 changed files with 81 additions and 54 deletions
  1. +2
    -1
      .gitignore
  2. +14
    -13
      Content/assets/World.scene.xml
  3. +1
    -1
      Dependencies/tracy
  4. BIN
      assets/World.blend
  5. BIN
      assets/World_PreTrackMods.blend
  6. +15
    -1
      src/Main.cpp
  7. +13
    -13
      src/PickUpObjective.cpp
  8. +33
    -24
      src/Terrain.cpp
  9. +3
    -1
      src/Terrain.hpp

+ 2
- 1
.gitignore View File

@ -55,4 +55,5 @@ hlmsDiskCache*
*.dae
Content/assets/**/*.geo
*.gltf
Collision/
Collision/
imgui.ini

+ 14
- 13
Content/assets/World.scene.xml View File

@ -1,16 +1,17 @@
<Model name="World" geometry="assets/World.geo">
<Mesh name="Loop-de-loop" material="assets/World_Material.002.material.xml" tx="129.411" ty="-5.15985" tz="161.816" sx="0.75" sy="0.75" sz="0.75" batchStart="0" batchCount="2448" vertRStart="0" vertREnd="2354" />
<Mesh name="Jump_Ramp2" material="assets/World_Material.002.material.xml" tx="-9.40073" ty="-1.41125" tz="-159.575" rx="2.91953" ry="0.160781" rz="-3.29351" sx="0.75" sy="0.75" sz="0.75" batchStart="2448" batchCount="36" vertRStart="2355" vertREnd="2386" />
<Mesh name="Jump_Ramp1" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-159.588" sx="0.75" sy="0.75" sz="0.75" batchStart="2484" batchCount="36" vertRStart="2387" vertREnd="2422" />
<Mesh name="Cube.008" material="assets/World_Material.002.material.xml" tx="-9.40073" ty="-1.64985" tz="-23.8973" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="2520" batchCount="36" vertRStart="2423" vertREnd="2446" />
<Mesh name="Cube.007" material="assets/World_Material.002.material.xml" tx="-9.38147" ty="-1.99989" tz="-17.0337" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="2556" batchCount="36" vertRStart="2447" vertREnd="2470" />
<Mesh name="Cube.006" material="assets/World_Material.002.material.xml" tx="-9.36327" ty="-2.33071" tz="-10.547" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="2592" batchCount="36" vertRStart="2471" vertREnd="2494" />
<Mesh name="Cube.005" material="assets/World_Material.002.material.xml" tx="-9.34776" ty="-2.61265" tz="-5.01873" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="2628" batchCount="36" vertRStart="2495" vertREnd="2518" />
<Mesh name="Cube.004" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-23.91" sx="0.75" sy="0.75" sz="0.75" batchStart="2664" batchCount="36" vertRStart="2519" vertREnd="2542" />
<Mesh name="Cube.003" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-17.0375" sx="0.75" sy="0.75" sz="0.75" batchStart="2700" batchCount="36" vertRStart="2543" vertREnd="2566" />
<Mesh name="Cube.002" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-10.5423" sx="0.75" sy="0.75" sz="0.75" batchStart="2736" batchCount="36" vertRStart="2567" vertREnd="2590" />
<Mesh name="Cube.001" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-5.00687" sx="0.75" sy="0.75" sz="0.75" batchStart="2772" batchCount="36" vertRStart="2591" vertREnd="2614" />
<Mesh name="BigCube" material="assets/World_Material.002.material.xml" tx="-58.9232" ty="0.7453" tz="-5.00687" sx="0.75" sy="0.75" sz="0.75" batchStart="2808" batchCount="36" vertRStart="2615" vertREnd="2638" />
<Mesh name="Ground" material="assets/World_Material.001.material.xml" tx="0.923551" ty="-0.348072" tz="0.13073" sx="0.75" sy="0.75" sz="0.75" batchStart="2844" batchCount="98304" vertRStart="2639" vertREnd="100942" />
<Mesh name="BezierCurve" material="assets/World_Material.001.material.xml" tx="-20.4536" ty="-3.45744" tz="-238.163" batchStart="0" batchCount="15495" vertRStart="0" vertREnd="15055" />
<Mesh name="Loop-de-loop" material="assets/World_Material.002.material.xml" tx="129.411" ty="-5.15985" tz="161.816" sx="0.75" sy="0.75" sz="0.75" batchStart="15495" batchCount="2448" vertRStart="15056" vertREnd="17410" />
<Mesh name="Jump_Ramp2" material="assets/World_Material.002.material.xml" tx="-9.40073" ty="-1.41125" tz="-159.575" rx="2.91953" ry="0.160781" rz="-3.29351" sx="0.75" sy="0.75" sz="0.75" batchStart="17943" batchCount="36" vertRStart="17411" vertREnd="17442" />
<Mesh name="Jump_Ramp1" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-159.588" sx="0.75" sy="0.75" sz="0.75" batchStart="17979" batchCount="36" vertRStart="17443" vertREnd="17478" />
<Mesh name="Cube.008" material="assets/World_Material.002.material.xml" tx="-9.40073" ty="-1.64985" tz="-23.8973" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="18015" batchCount="36" vertRStart="17479" vertREnd="17502" />
<Mesh name="Cube.007" material="assets/World_Material.002.material.xml" tx="-9.38147" ty="-1.99989" tz="-17.0337" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="18051" batchCount="36" vertRStart="17503" vertREnd="17526" />
<Mesh name="Cube.006" material="assets/World_Material.002.material.xml" tx="-9.36327" ty="-2.33071" tz="-10.547" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="18087" batchCount="36" vertRStart="17527" vertREnd="17550" />
<Mesh name="Cube.005" material="assets/World_Material.002.material.xml" tx="-9.34776" ty="-2.61265" tz="-5.01873" rx="2.91953" ry="0.160781" rz="-0.126519" sx="0.75" sy="0.75" sz="0.75" batchStart="18123" batchCount="36" vertRStart="17551" vertREnd="17574" />
<Mesh name="Cube.004" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-23.91" sx="0.75" sy="0.75" sz="0.75" batchStart="18159" batchCount="36" vertRStart="17575" vertREnd="17598" />
<Mesh name="Cube.003" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-17.0375" sx="0.75" sy="0.75" sz="0.75" batchStart="18195" batchCount="36" vertRStart="17599" vertREnd="17622" />
<Mesh name="Cube.002" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-10.5423" sx="0.75" sy="0.75" sz="0.75" batchStart="18231" batchCount="36" vertRStart="17623" vertREnd="17646" />
<Mesh name="Cube.001" material="assets/World_Material.002.material.xml" tx="-32.1332" ty="-0.666559" tz="-5.00687" sx="0.75" sy="0.75" sz="0.75" batchStart="18267" batchCount="36" vertRStart="17647" vertREnd="17670" />
<Mesh name="BigCube" material="assets/World_Material.002.material.xml" tx="-58.9232" ty="0.7453" tz="-5.00687" sx="0.75" sy="0.75" sz="0.75" batchStart="18303" batchCount="36" vertRStart="17671" vertREnd="17694" />
<Mesh name="Ground" material="assets/World_Material.001.material.xml" tx="0.923551" ty="-0.348072" tz="0.13073" sx="0.75" sy="0.75" sz="0.75" batchStart="18339" batchCount="98304" vertRStart="17695" vertREnd="115998" />
</Model>

+ 1
- 1
Dependencies/tracy

@ -1 +1 @@
Subproject commit 47ef56b995d9d1396ec1eba6121ff230e5db07f3
Subproject commit 1650d371e613b7884f60b53b2cd24548b5ca83fe

BIN
assets/World.blend View File


BIN
assets/World_PreTrackMods.blend View File


+ 15
- 1
src/Main.cpp View File

@ -14,6 +14,7 @@
#include <glm/gtx/norm.hpp> // distance2
#include <glm/gtx/vector_angle.hpp> // angle
#include <glm/mat4x4.hpp> // mat4
#include <glm/vec3.hpp>
#include <map>
#include <sstream>
@ -308,7 +309,20 @@ int main()
// Terrain
{
createCollisionHeightfield(physicsWorld);
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 0.f)));
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 1.f)));
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 2.f)));
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 3.f)));
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 4.f)));
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 5.f)));
createCollisionHeightfield(physicsWorld,
glm::vec3(-17.f, -30.f, 250.f + ((20.f * 5.f) * 6.f)));
}
// objTest();


+ 13
- 13
src/PickUpObjective.cpp View File

@ -38,9 +38,9 @@ struct
PhysicsWorld* cachedPhysicsWorld = nullptr;
timer objectiveTimer;
float completionTime = 0.f;
// UI
text displayText;
int numObjectivesHit = 0;
@ -84,19 +84,21 @@ void Initialize(PhysicsWorld* physicsWorld)
// Blender coords to ours: swap Y and Z, then invert Z
glm::vec3 objectivePositions[] = {
// Near start
glm::vec3(20.f, 0.f, 0.f),
glm::vec3(15.f, 0.f, 0.f),
// Ramps
glm::vec3(-39.91f, 13.25f, -214.2f),
glm::vec3(-29.93f, 9.93f, -160.64f),
// Left pinch
glm::vec3(125.2f, 39.62f, -66.95f),
glm::vec3(93.89f, 29.71f, -50.21f),
// Box
glm::vec3(-78.82f, 15.01f, -6.5f),
glm::vec3(-59.11f, 11.25f, -4.87f),
// Hidden butt
glm::vec3(249.6f, 3.254f, -128.6f),
glm::vec3(187.20f, 2.44f, -96.45f),
// Big hill jump
glm::vec3(-125.4f, 37.37f, -0.975f),
// Loop-de-loop
glm::vec3(134.8f, 24.24f, 212.3f),
glm::vec3(-94.05f, 28.02f, -0.73f),
// Loop-de-loop
glm::vec3(101.1f, 18.18f, 159.2f),
// Race track
glm::vec3(-63.f, -2.5f, -505.f),
};
// Objectives
@ -105,9 +107,7 @@ void Initialize(PhysicsWorld* physicsWorld)
for (unsigned int i = 0; i < ArraySize(objectivePositions); ++i)
{
glm::mat4 objectiveLocation = glm::translate(glm::mat4(1.f), objectivePositions[i]);
// NOT Normal! Only because I rescaled the world
glm::mat4 objectiveScale = glm::scale(glm::mat4(1.f), glm::vec3(0.75f));
g_PickUpObjectivesState.objectives[i].renderObject.SetTransform(objectiveScale * objectiveLocation);
g_PickUpObjectivesState.objectives[i].renderObject.SetTransform(objectiveLocation);
g_PickUpObjectivesState.objectives[i].renderObject.Initialize("PickUp");
}


+ 33
- 24
src/Terrain.cpp View File

@ -1,6 +1,7 @@
#include "Terrain.hpp"
#include <algorithm>
#include <cstdio> // sprintf
#include <limits>
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
@ -19,18 +20,17 @@
#include "PhysicsWorld.hpp"
// This must be crap is probably not true I think
static int s_gridSize = 20 + 1; // must be (2^N) + 1
// static int s_gridSize = 16 + 1; // must be (2^N) + 1
// static int s_gridSize = 8 + 1; // must be (2^N) + 1
int g_TerrainGridSize = 20 + 1; // must be (2^N) + 1
float scaleHeightfield = 5.0f;
// static int g_TerrainGridSize = 16 + 1; // must be (2^N) + 1
// static int g_TerrainGridSize = 8 + 1; // must be (2^N) + 1
// static btScalar s_gridSpacing = 1.0;
static const int upAxis = 1;
static const int worldSeed = 51388234;
static Noise2d noiseGenerator(worldSeed);
// TODO This is fucked up
HeightfieldCell* g_rawHeightfieldData = new HeightfieldCell[s_gridSize * s_gridSize];
class TerrainTriangleCollector : public btTriangleCallback
{
public:
@ -83,7 +83,7 @@ public:
// TODO Memory leaks
// Note that heightfield data is not owned by Bullet
void createCollisionHeightfield(PhysicsWorld& world)
void createCollisionHeightfield(PhysicsWorld& world, const glm::vec3& worldPosition)
{
PerfTimeNamedScope(terrainInit, "Terrain creation", tracy::Color::LawnGreen);
@ -92,26 +92,31 @@ void createCollisionHeightfield(PhysicsWorld& world)
// getRawHeightfieldData(model, type, minHeight, maxHeight);
// btAssert(g_rawHeightfieldData && "failed to create raw heightfield");
float xOffset = 0.f;
float zOffset = 0.f;
// Do noise in unscaled space so that changing the scale doesn't necessitate a change of neise
const float xNoiseSpaceOffset = worldPosition[0] / scaleHeightfield;
const float zNoiseSpaceOffset = worldPosition[2] / scaleHeightfield;
// TODO: Leak!
HeightfieldCell* rawHeightfieldData =
new HeightfieldCell[g_TerrainGridSize * g_TerrainGridSize];
float minY = 0.f;
float maxY = 0.f;
{
PerfTimeNamedScope(terrainNoise, "Terrain noise generation", tracy::Color::ForestGreen);
for (int cellZ = 0; cellZ < s_gridSize; cellZ++)
for (int cellZ = 0; cellZ < g_TerrainGridSize; cellZ++)
{
for (int cellX = 0; cellX < s_gridSize; cellX++)
for (int cellX = 0; cellX < g_TerrainGridSize; cellX++)
{
float noiseScale = 0.7f;
float noiseX = (cellX + xOffset) * noiseScale;
float noiseZ = (cellZ + zOffset) * noiseScale;
float noiseX = (cellX + xNoiseSpaceOffset) * noiseScale;
float noiseZ = (cellZ + zNoiseSpaceOffset) * noiseScale;
float value = noiseGenerator.scaledOctaveNoise2d(noiseX, noiseZ, 0.f, 10.f, 4, 0.1f,
0.22f, 2.f);
g_rawHeightfieldData[(cellZ * s_gridSize) + cellX] = value;
rawHeightfieldData[(cellZ * g_TerrainGridSize) + cellX] = value;
minY = std::min(minY, value);
maxY = std::max(maxY, value);
}
@ -121,10 +126,10 @@ void createCollisionHeightfield(PhysicsWorld& world)
bool flipQuadEdges = false;
const PHY_ScalarType heightfieldDataType = PHY_FLOAT;
// HeightScale is ignored when using the float heightfield data type.
btScalar gridHeightScale = 1.0;
btHeightfieldTerrainShape* heightfieldShape =
new btHeightfieldTerrainShape(s_gridSize, s_gridSize, g_rawHeightfieldData, gridHeightScale,
minY, maxY, upAxis, heightfieldDataType, flipQuadEdges);
btScalar gridHeightScale = 1.f;
btHeightfieldTerrainShape* heightfieldShape = new btHeightfieldTerrainShape(
g_TerrainGridSize, g_TerrainGridSize, rawHeightfieldData, gridHeightScale, minY, maxY,
upAxis, heightfieldDataType, flipQuadEdges);
// BuildAccelerator is optional, it may not support all features.
// Builds a grid data structure storing the min and max heights of the terrain in chunks.
@ -132,16 +137,17 @@ void createCollisionHeightfield(PhysicsWorld& world)
// If you modify the heights, you need to rebuild this accelerator.
heightfieldShape->buildAccelerator();
btScalar scaleHeightfield = 5.0f;
heightfieldShape->setLocalScaling(
btVector3(scaleHeightfield, scaleHeightfield, scaleHeightfield));
// Set origin to middle of heightfield
btTransform transform;
transform.setIdentity();
// Note that this origin will be center of heightfield, so it will need to be adjusted if bounds
// change
transform.setOrigin(btVector3(-17.f, -5.f, 250.f));
// Bullet computes the origin of the heightfield to be in the center. Because the geometry is
// procedural, we need to adjust its transform up or down based on where the origin ended up
glm::vec3 adjustedOrigin(worldPosition);
adjustedOrigin[1] += ((maxY - minY) / 2.f) * scaleHeightfield;
transform.setOrigin(glmVec3ToBulletVector(adjustedOrigin));
// btRigidBody* body =
world.localCreateRigidBody(PhysicsWorld::StaticRigidBodyMass, transform, heightfieldShape);
@ -178,8 +184,11 @@ void createCollisionHeightfield(PhysicsWorld& world)
// indices.size());
Graphics::ProceduralMesh graphicsMesh;
graphicsMesh.Initialize("Heightfield", vertices.data(), indices.data(), nullptr,
nullptr, nullptr, uvs.data(), nullptr, vertices.size() / 3,
char nameBuffer[64];
snprintf(nameBuffer, sizeof(nameBuffer), "Heightfield_%d_%d", (int)worldPosition[0],
(int)worldPosition[2]);
graphicsMesh.Initialize(nameBuffer, vertices.data(), indices.data(), nullptr, nullptr,
nullptr, uvs.data(), nullptr, vertices.size() / 3,
indices.size());
// Note that scaling happens in processAllTriangles(), not from the transform
graphicsMesh.SetTransform(BulletTransformToGlmMat4(transform));


+ 3
- 1
src/Terrain.hpp View File

@ -2,8 +2,10 @@
#include "btBulletDynamicsCommon.h"
#include <glm/vec3.hpp>
class PhysicsWorld;
void createCollisionHeightfield(PhysicsWorld& world);
void createCollisionHeightfield(PhysicsWorld& world, const glm::vec3& worldPosition);
// Important: Note that even if the type is called PHY_"FLOAT", this needs to be btScalar (i.e. can
// end up being a double)


Loading…
Cancel
Save