SpiderPig Posted September 6, 2023 Share Posted September 6, 2023 I'm not sure how to go about this. I think what I need is one of the transform functions. This is a simplified example of my voxel terrain where each box represents a component of the terrain. Holding 'space' will replace random boxes. While all the boxes in this example exist, in the voxel terrain not all components exist at once, they can be created for the first time when the player edits the terrain, so I can't just grab the existing boxes rotation and use that for the new box. I only have the pivots position and rotation to decided what the new boxes position and rotation should be relative to the pivot. The position of the new box is okay if I set it's position to be local. I wonder if the rotation should be the same? #include "UltraEngine.h" using namespace UltraEngine; Vec3 mousepos = Vec3(0.0f); float move_adjustment = 0.1f; float move_speed = 1.0f; float lookspeed = 0.1f; float looksmoothing = 0.5f; bool enable_camera = false; int main(int argc, const char* argv[]) { auto displays = GetDisplays(); auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR); auto world = CreateWorld(); auto framebuffer = CreateFramebuffer(window); auto camera = CreateCamera(world); camera->SetClearColor(0.125); camera->SetFov(70); camera->SetPosition(0, 10, -5); auto light = CreateDirectionalLight(world); light->SetRotation(35, 45, 0); auto floor = CreateBox(world, 100.0f, 0.1f, 100.0f); floor->SetPosition(0.0f, -3.0f, 0.0f); const int grid_size = 3; const int total_cubes = grid_size * grid_size * grid_size; shared_ptr<Entity> rubix_cube[total_cubes]; auto material = CreateMaterial(); material->SetShaderFamily(LoadShaderFamily("Shaders\\Unlit.fam")); auto pivot = CreatePivot(world); pivot->SetShadows(true); int id = 0; for (int z = 0; z < grid_size; z++) { for (int y = 0; y < grid_size; y++) { for (int x = 0; x < grid_size; x++) { rubix_cube[id] = CreateBox(world); rubix_cube[id]->SetColor(Random(), Random(), Random()); rubix_cube[id]->SetParent(pivot); rubix_cube[id]->SetPosition(x - 1, y - 1, z - 1); id++; } } } //Main loop bool stage = 0; while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false) { if (window->KeyHit(KEY_F2) == true) { camera->SetWireframe(!camera->GetWireframe()); } if (window->KeyHit(KEY_F3) == true) { camera->SetDebugPhysicsMode(!camera->GetDebugPhysicsMode()); } if (window->KeyHit(KEY_F4) == true) { enable_camera = !enable_camera; } //Rotate the pivot auto pos = pivot->GetPosition(true); pivot->Turn(0.025f, 0.025f, 0.025f); switch (stage) { case 0: if (pos.x < 5.0f) { pivot->Move(0.01f, 0.0f, 0.0f); } else { stage = 1; } break; case 1: if (pos.x > 0.0f) { pivot->Move(-0.01f, 0.0f, 0.0f); } else { stage = 0; } break; } //Randomly Replace a Box if (window->KeyDown(KEY_SPACE) == true) { auto x = Floor(Random(2.5f)); auto y = Floor(Random(2.5f)); auto z = Floor(Random(2.5f)); id = (z * (grid_size * grid_size)) + (y * grid_size) + x; rubix_cube[id]->SetParent(nullptr);//Should this be needed? rubix_cube[id] = CreateBox(world); rubix_cube[id]->SetColor(Random(), Random(), Random()); rubix_cube[id]->SetParent(pivot); rubix_cube[id]->SetPosition(x - 1, y - 1, z - 1); //How to transform to keep the rotation and position. //Can't use current rotation and position of box //In my game there is not allways a model present at that location //Nope //auto new_r = TransformRotation(Vec3(0.0f) , Mat4(), pivot->GetMatrix()); //rubix_cube[id]->SetRotation(new_r); } if (enable_camera) { auto _displaySize = window->GetSize(); float cx = Round((float)_displaySize.x / 2.0f); float cy = Round((float)_displaySize.y / 2.0f); auto mpos = Vec3(window->GetMousePosition().x, window->GetMousePosition().y, window->GetMousePosition().z); window->SetMousePosition(cx, cy); mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing); auto dx = (mpos.x - cx) * lookspeed; auto dy = (mpos.y - cy) * lookspeed; auto camrot = camera->GetRotation(); camrot.x += dy; camrot.y += dx; camera->SetRotation(camrot); mousepos = mpos; auto speed = 0.1f; if (window->KeyDown(KEY_SHIFT) == true) { speed = speed * 10.0f; } if (window->KeyDown(KEY_W) == true) { camera->Move(0, 0, speed); } else if (window->KeyDown(KEY_S) == true) { camera->Move(0, 0, -speed); } if (window->KeyDown(KEY_A) == true) { camera->Move(-speed, 0, 0); } else if (window->KeyDown(KEY_D) == true) { camera->Move(speed, 0, 0); } } world->Update(); world->Render(framebuffer); } return 0; } Quote Link to comment Share on other sites More sharing options...
Solution Josh Posted September 6, 2023 Solution Share Posted September 6, 2023 I can't tell what you are trying to do. When you call SetParent with the last parameter set to true (the default), the entity's global orientation is preserved. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
SpiderPig Posted September 6, 2023 Author Share Posted September 6, 2023 That was it. Using false with SetParent(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.