gamecreator Posted March 27, 2018 Share Posted March 27, 2018 This should be really simple but I'm not sure what I'm doing wrong. I'm trying to make it so that my character jumps the same height on different computers. I made a really simple test program and below is the entire code. The problem is that the player jumps less and less high as the time in the sleep function is increased. What am I doing wrong? (I also just tried the Leadwerks Time::Delay function and it does the same thing.) #include "Leadwerks.h" using namespace Leadwerks; Light* light = NULL; float vy=0, y=0.4; float highesty=0; int main(int argc, const char *argv[]) { Leadwerks::Window* window = Leadwerks::Window::Create(); Context* context = Context::Create(window); World* world = World::Create(); Camera* camera = Camera::Create(); camera->SetRotation(0, 0, 0); camera->Move(0, 3, -6); Light* light = DirectionalLight::Create(); light->SetRotation(35, 35, 0); Model* ground = Model::Box(10, 1, 1); ground->SetColor(0.0, 0.5, 0.0); ground->SetPosition(0, -1, 0); Model* cliff = Model::Box(2, 5, 1); cliff->SetColor(0.3, 0.4, 0.9); cliff->SetPosition(2, 2, 0); Model* player = Model::Box(1, 1.8, 1); player->SetColor(1.0, 1.0, 1.0); player->SetPosition(0, 0.4, 0); while (true) { if(window->Closed() || window->KeyDown(Key::Escape)) return false; if(window->KeyHit(Key::Space)) // Jump pressed { vy=0.8; highesty=0; // Reset highest jump tracker } if(y>highesty) highesty=y; // Keep track of highest position vy-=0.05*Time::GetSpeed(); // Gravity y+=vy*Time::GetSpeed(); // Add velocity to y if(y<0.4) y=0.4; // Stop at ground player->SetPosition(0, y, 0); Leadwerks::Time::Update(); world->Update(); world->Render(); context->SetBlendMode(Blend::Alpha); context->SetColor(1.0, 1.0, 1.0); context->DrawText("highest y: "+String(highesty), 10, 10); context->DrawText("GetSpeed: "+String(Time::GetSpeed()), 10, 30); context->Sync(); Sleep(1); } return 0; } 1 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted March 27, 2018 Share Posted March 27, 2018 Do you get the desired result when the Sleep() function is commented out? What is the output of this added line: context->DrawText("Gravity: "+String(vy), 10, 50); Quote Link to comment Share on other sites More sharing options...
gamecreator Posted March 27, 2018 Author Share Posted March 27, 2018 18 hours ago, AggrorJorn said: Do you get the desired result when the Sleep() function is commented out? The Sleep function is what helps me test whether the jump is consistent. I can change any of the other numbers. But I'm hoping to have the character jump the same height with Sleep(1) (or no Sleep line at all) versus Sleep(100) versus Sleep(20). 18 hours ago, AggrorJorn said: What is the output of this added line: context->DrawText("Gravity: "+String(vy), 10, 50); It shows the y velocity steadily decreasing (being effected by "gravity"). Edit: I tested this with simply with a box moving right and everything went as expected (almost the exact same distance no matter the FPS). So I'm pretty sure my code is somehow wrong but I don't see how. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 28, 2018 Share Posted March 28, 2018 Intuitively it strikes me like there is something wrong with multiplying acceleration by the execution time, but I am not sure. Whenever I apply acceleration in the engine it is on a fixed timestep, so I have never thought about this. 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...
gamecreator Posted March 28, 2018 Author Share Posted March 28, 2018 9 hours ago, Josh said: Intuitively it strikes me like there is something wrong with multiplying acceleration by the execution time, but I am not sure. Yeah, it seems like double dipping and I agree that it's probably wrong. But that's the way it gets closest to the same height at all FPS. If I only do it when I add to y, the character jumps super high when frame rates are low. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 29, 2018 Share Posted March 29, 2018 Acceleration is in meters per second squared so I wonder if it's a square root or something. It's funny how something so simple can be so unintuitive when you have never actually worked it out. 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...
Solution gamecreator Posted March 29, 2018 Author Solution Share Posted March 29, 2018 I ended up using fixed timestep code from here: https://gafferongames.com/post/fix_your_timestep/ 1 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.