Jump to content

Time Warp Failure


Shard
 Share

Recommended Posts

I'm working on a small little side project I've named Time Warp.

Basically the idea is to keep track of position, rotation and force for each frame of an object and then in the rewind play it back by setting the object to those values.

 

Everything works, except for the force. Below is a video.

 

http://www.youtube.com/watch?v=Rdysi8XnbvA

 

 

For some reason the boxes would "pop" and go flying all different ways.

So I tried to set the forces, but that lead to a new problem.

When the boxes were done moving, they would shoot off in a random direction, as if all the forces supplied were applied at once.

Below is my code with the Cap ([Time] Capsule) object.

 

#include "leo.h"
using namespace LEO;

#include <stdio.h>
#include <vector>
using namespace std;

struct Cap
{
BodyBox *body; 
Mesh mesh;
vector<TVec3> rot;
vector<TVec3> pos;
vector<TVec3> force;
bool timeForward;

Cap()
{
	body = new BodyBox;

	timeForward = true;

	body->Create(1,1,1);
	body->SetMass(1);
	body->SetType(1);
}

void Update()
{
	if(timeForward) Forward();
	else Backward();
}

void Forward()
{
	rot.push_back(body->GetRotation());
	pos.push_back(body->GetPosition());
	force.push_back(body->GetVelocity());
}

void Backward()
{
	if(pos.size() > 0)
	{
		this->body->SetPosition(pos.back());
		this->body->SetRotation(rot.back());
		SetBodyForce(*this->body,force.back());

		pos.pop_back();
		rot.pop_back();
		force.pop_back();
	}

	else Swap();
}

void Swap()
{
	//If forward then set to backward
	if(timeForward) 
	{
		body->SetGravityMode(0);
		timeForward = false;
	}

	else 
	{
		body->SetGravityMode(1);
		timeForward = true;
	}
}
};

int main(int argc, char** argv)
{
Engine engine("Time Warp", 800, 600);

Framework frameWork;
frameWork.Create();
frameWork.SetStats(2);

Camera *camera;
camera = &frameWork.GetMain().GetCamera();

//Set Lua variable
BP L=GetLuaState();
lua_pushobject(L,frameWork);
lua_setglobal(L,"fw");
lua_pop(L,1);

//Create Light
DirectionalLight light;
light.Create();



//Ground
BodyBox ground;
ground.Create(1,1,1);
ground.SetScale(Vec3(10,1,10));
ground.SetPosition(Vec3(0,-3,0));
ground.SetType(1);

camera->SetPosition(0,20,-80);

DebugPhysics();

vector<Cap*> boxes;

float x; 
float z; 
for ( int n=1 ; n<=100; n++ ) 
{ 
	Cap *bod = new Cap;
	x = -5+((float)rand()/RAND_MAX)*10; 
	z = -5+((float)rand()/RAND_MAX)*10; 
	bod->body->SetPosition(Vec3(x,n,z) ); 
	boxes.push_back(bod);
} 


bool timeForward = true;
bool resume = false;

//Main loop 
while(!KeyHit(KEY_ESCAPE)) 
{ 

	if(KeyHit(KEY_SPACE))
	{
		for(int i = 0; i < boxes.size(); i++) boxes[i]->Swap();
	}

	for(int i = 0; i < boxes.size(); i++) 
	{
		boxes[i]->Update();
	}


	//Update the world 
	frameWork.Update();
	frameWork.Render();

	//Swap the front and back buffer 
	Flip(); 
} 

return engine.Free(); 
} 

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

I seem to remember having the same issue if the physics update method wasn't being called over frames it would build up the force values still and when it was called apply all at once which resulted in some crazy stuff.

 

I would think in rewind mode, that you would simply set position/rotation only. Would there really be a need to set the force when rewinding things?

Link to comment
Share on other sites

Hi!

I think Ive found the problem.

In Frward() you record body velocity. But in Rewind() you Set body force. Replace it to SetBodyVelocity() and it should work.

 

[Edit]

And also you should replace

frameWork.Update()

to:

frameWork.main.world.Update(1);
frameWork.background.world.Update(1);
frameWork.transparency.world.Update(1);

 

If you call just frameWork.Update() it will work as Rick mentioned.

Q6600@2.4GHz - 9600GT - 4GB DDR2@800MHz - Windows7 x64

3ds max / photoshop CS3 / C++

http://www.arbuznikov.com

Link to comment
Share on other sites

I would think in rewind mode, that you would simply set position/rotation only. Would there really be a need to set the force when rewinding things?

 

It would. Think about a rocket. Its moving forward with a force. If I don't set that, then when it stops rewinding, it won't have the forward force and just fall to the ground.

 

Hi!

I think Ive found the problem.

In Frward() you record body velocity. But in Rewind() you Set body force. Replace it to SetBodyVelocity() and it should work.

 

[Edit]

And also you should replace

frameWork.Update()

to:

frameWork.main.world.Update(1);
frameWork.background.world.Update(1);
frameWork.transparency.world.Update(1);

 

If you call just frameWork.Update() it will work as Rick mentioned.

 

I fixed the velocity and then added the omega which made it work perfectly.

 

I did change my update, but now physics happens much faster (more realistically?)

What is the difference between this and just a regular update?

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

its about world updating. http://www.leadwerks.com/wiki/index.php?title=Worlds#UpdateWorld

 

If you use framewor.Update() it will call world.Update(Engene::GetSpeed()) for each world. The physics in this case will be frame independent. So for example if your program is out of focus, and then in focus again world.Update() will update physics to correspond the time that the program wasn't active. This means that all forces will be multiplied by the delay of the program. It is like physics didnt stop while the program is suspended. I hope that I explained it clear ( Sorry for my English :))

If you call world.Update(1) then physics will "stop" when the program is suspended. After you're in focus again the physics will just continue as it was.

 

As I understand its made for physics to be frame rate independent. But from my little experience, I figured that its not suitable in most cases.

Q6600@2.4GHz - 9600GT - 4GB DDR2@800MHz - Windows7 x64

3ds max / photoshop CS3 / C++

http://www.arbuznikov.com

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...