Jump to content

GetSpeed Help


gamecreator
 Share

Go to solution Solved by gamecreator,

Recommended Posts

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;
}

 

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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

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.

Link to comment
Share on other sites

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.

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...