Jump to content

  • Log In with Google      Sign In   
  • Create Account

Solid_Spy

Member Since 26 Nov 2012
Offline Last Active Yesterday, 09:43 PM

Posts I've Made

In Topic: How to compile libraries for C++ MinGW with MSYS on Windows - for indie game...

16 December 2014 - 05:38 PM

Codeblocks?

It could also work for codeblocks I think. Don't see any reason why not.


In Topic: problem with variable time, updating objects created on the same frame at dif...

15 December 2014 - 03:13 PM

 

Some options:

  • Keep track of the time step separately for each object. Default to 10ms (or your global time step), but allow other intervals to be specified (so that an object only simulates for part of the step).
  • Advance the whole simulation with small sub-timesteps until the next object creation event occurs (e.g. 1ms). Then advance by the time until the next object creation event occurs (e.g. 2ms - 1ms = 1ms). Next: (5ms - 2ms = 3ms), then (10ms - 5ms = 5ms) to complete the global time step. This is similar to the approach used for continuous collision detection that finds the next time of collision.

 

I used option one, and it seemed to have worked I believe :3.

	while(!glfwWindowShouldClose(window))
	{
		QueryPerformanceCounter(&currentTime);

		counterAmmount += deltaTime;

		while(counterAmmount >= 20.0)
		{
			float relativeDeltaTime = counterAmmount - 20.0f;
			counterAmmount -= 20.0;

			float scaleAmmount = (static_cast<float>(rand() % 1000) / 1000.0f);
			LARGE_INTEGER timeStamp;
			scaleAmmount = scaleAmmount + 0.5f / 2.0f;
			particle particle1;
			particle1.alive = true;
			particle1.lifeTime = 2000.0f;
			particle1.x = 0.0f;
			particle1.y = -2.0f;
			particle1.z = 0.0f;
			particle1.sx = /*scaleAmmount*/1.0;
			particle1.sy = /*scaleAmmount*/1.0;
			particle1.sz = /*scaleAmmount*/1.0;
			particle1.dx = /*(static_cast<float>((rand() % 1000) - 500) / 1000.0f)*/0.0f;
			particle1.dy = /*(static_cast<float>((rand() % 1000)) / 1000.0f)*/ 0.5f;
			particle1.dz = 1.0f;
			particle1.r = (static_cast<float>((rand() % 1000)) / 1000.0f);
			particle1.g = (static_cast<float>((rand() % 1000)) / 1000.0f);
			particle1.b = (static_cast<float>((rand() % 1000)) / 1000.0f);
			particle1.timeSinceCreated = counterAmmount;

			particleList.push_back(particle1);
		}

		for(std::vector<particle>::iterator iterator = particleList.begin();
			iterator != particleList.end();
			iterator++)
		{
			if(iterator->lifeTime <= 0.0f)
			{
				std::vector<particle>::iterator myAss2 = particleList.begin() + (particleList.size() - 1);
				std::iter_swap(iterator, myAss2);
				particleList.pop_back();
				iterator--;
			}
			else
			{
				if(iterator->timeSinceCreated == 0.0f)
				{
					iterator->timeSinceCreated = deltaTime;
				}
				iterator->sx -= 0.001f * iterator->timeSinceCreated;
				iterator->sy -= 0.001f * iterator->timeSinceCreated;
				iterator->sz -= 0.001f * iterator->timeSinceCreated;
				if(iterator->sx <= 0.0f)
				{
					iterator->sx = 0.0f;
					iterator->sy = 0.0f;
					iterator->sz = 0.0f;
				}
				iterator->x += (iterator->dx * (0.01f)) * iterator->timeSinceCreated;
				iterator->y += (iterator->dy * (0.01f)) * iterator->timeSinceCreated;
				iterator->lifeTime -= (1.0f) * iterator->timeSinceCreated;

				iterator->timeSinceCreated = 0.0f;
			}
		}

In Topic: VERY weird error. Structs in std::vector being updated more than once

15 December 2014 - 02:01 AM

I officially got it right this time:

		for(std::vector<particle>::iterator iterator = particleList.begin();
			iterator != particleList.end();
			iterator++)
		{
			if(iterator->lifeTime <= 0.0f)
			{
				std::vector<particle>::iterator myAss2 = particleList.begin() + (particleList.size() - 1);
				std::iter_swap(iterator, myAss2);
				//particleList.erase(iterator);
				particleList.pop_back();
				iterator--;
				//outFile << "removed one";
			}
			else
			{
				iterator->y += 0.1f;
				//outFile << iterator->y;
				//outFile << " ";
				iterator->lifeTime -= 1.0f;
			}
			//outFile << "\n";
		}

I debugged it and now I am getting the correct information. The only problem seems to be the rendering, which is a separate issue.


In Topic: VERY weird error. Structs in std::vector being updated more than once

15 December 2014 - 12:48 AM

++iterator won't fix it. The problem is you use iterator in the same loop iteration in which you decremented iterator. It causes you to go back to the previous iteration value too early. By the time the execution reaches the top of the loop the error has already occurred. Using erase would fix the issue, but erase might be slow if you have a large number of particles. Your swap and pop method is superior from a performance perspective. Try this:

        for(unsigned int iterator = 0;
            iterator < currentSize;
            iterator++)
        {
            particleList[iterator].lifeTime -= 1.0f;
            if(particleList[iterator].lifeTime <= 0.0f)
            {
                std::swap(particleList[iterator], particleList[particleList.size() - 1]);
                particleList.pop_back();
                iterator--;
                currentSize = particleList.size();
            }
            else
            {
                particleList[iterator].y += 0.01f;
            }
        }

I moved currentSize so it only gets calculated when you update the size. The more important change is that since you either want to delete the current particle or you want to increment the y value so I put the y increment in the else statement which you already had!

 

And slight addendum to my '-1' bug comment. You use an unsigned int so it wouldn't be negative, it would be very large.

 

edit: edited for clarity.

Unfortunately I tried this and it didn't work either. I tried different combinations and tried placing the statements in different places. I have an itching feeling that it may be an issue with the swap function or the pop function., cuz I never have problems doing these kind of loops in python or other languages, where I decrement the iterator inside the loop so I don't skip elements.


In Topic: VERY weird error. Structs in std::vector being updated more than once

15 December 2014 - 12:31 AM

Ok, well I managed to fix it by using

 

        for(unsigned int iterator = 0;
            iterator < currentSize;
            iterator++)
        {
            particleList[iterator].lifeTime -= 1.0f;
            if(particleList[iterator].lifeTime <= 0.0f)
            {
                std::swap(particleList[iterator], particleList[particleList.size() - 1]);
                particleList.pop_back();
                iterator--;
            }
            else
            {
            }

            particleList[iterator].y += 0.01f;

            currentSize = particleList.size();
        }

You should consider using your debugger and stepping through the code. I think I have a handle on what might be going on though. First, let's say you have particleList[0].lifeTime == 1.0f and then you enter this loop. iterator is 0 so particleList[0].lifeTime will decrement to lifeTime 0.0f so you swap and pop the element then decrement interator to -1. Then you access particleList[-1].y which doesn't exist so who knows what your program will do if it tries to access that memory.

 

Now, I also think this bug is causing your issues with the weird multiple increases. If particleList[0].lifeTime == 2.0f and particleList[1].lifeTime == 1.0f then at the start of the loop when iterator == 0, particleList[0] will remain in the list and particleList[0].y will increment by 0.01f. iterator will increment to 1. So next particleList[1] will be swapped and deleted because its lifetime reaches 0. When this happens, iterator is set to 0 and particleList[0].y will increment by 0.01f again for 0.02f total.

 

So, exercise for the reader: solve this bug!

 

Ah, I thought that it would increment as soon as it re-entered the for loop. I guess I dun goofed. I think I have to use ++iterator instead of iterator++. Either way I solved it by using particleList.erase(). I will try the ++ method thoush. Update: it didn't work :P.


PARTNERS