Jump to content

  • Log In with Google      Sign In   
  • Create Account

lightxbulb

Member Since 20 Apr 2013
Online Last Active Today, 10:17 AM

Topics I've Started

Physics - bouncing ball, error accumulation

12 June 2013 - 06:30 AM

Hi!

I've been doing a simulation of a perfect case of a ball bouncing against a flat plane(representing the ground) accelerating under the force of gravity. Here are the important parts: 

I chose one of the simplest cases where the gravity vector is equal to 1 and points down the Y axis(opposite direction - basically points at the ground). The ball starts in mid air with a velocity vector equal to 0. It accelerates towards the "ground" under the force of gravity and bounces back up - then the whole thing repeats. Here's something that might help me explain what I want:

Collision normal vector: n

Velocity vector of the ball: v

Gravity vector: g

 

Here's how I calculate how the ball's velocity vector changes after the collision with the ground(it is basically mirrored around the normal):

v1 = v -2*(v dot n)*n, where (v dot n) is the dot product between 'v' and 'n'. For every iteration of the main loop I do an update for the velocity of the ball. It is important to note that in this simple case:

n(0,1,0) ; v(0, x, 0); g(0, -1, 0)

 

I've got some function like this in the main loop:

Function updateMovement(in.Body, world.Physics, deltaTime#)
	
	If( EntityCollided(in\obj, colWorld) ) Then
		
		Local nx# = CollisionNX(in\obj)
		Local ny# = CollisionNY(in\obj)
		Local nz# = CollisionNZ(in\obj)
		
		Local vPn# = in\vx*nx + in\vy*ny + in\vz*nz
		
		in\vx = in\vx - 2*vPn*nx
		in\vy = in\vy - 2*vPn*ny
		in\vz = in\vz - 2*vPn*nz
		
	EndIf
	in\vx = in\vx + world\gravityx*deltaTime
	in\vy = in\vy + world\gravityy*deltaTime
	in\vz = in\vz + world\gravityz*deltaTime
	
	TranslateEntity in\obj,TranslateEntity in\obj, in\vx*deltaTime*world\speedScale, in\vy*deltaTime*world\speedScale, in\vz*deltaTime*world\speedScale
End Function 

Don't mind really the strange syntax - the various variables are:

g(world\gravityx, world\gravityy, world\gravityz);  v(in\vx, in\vy, in\vz), n(nx, ny, nz)

deltaTime is the average time it takes for 1 frame.

world\speedScale is just a coefficient that scales the movement for the ball.

TranslateEntity is the function that "makes the ball move" given an entity and a vector in the format TranslateEntity entity,x,y,z.

CollisionN* returns the normal for the collision. EntityCollided evaluates to true if there is a collision between the ball and the plane.

 

So it's something like:

void updateMovement()
{
    //if the ball touches the ground:
    //change the velocity vector of the ball accordingly(basically a bounce off)
    //it is important to note that the velocity vector's size doesn't change -
    //just its direction
    //end if

    //calculate the change in speed
    //move the ball accordingly
}

The problem is error accumulates with every bounce off the ground and rather than have the ball retaining a constant energy level(kinetic+potential) it starts gaining some. If I make deltaTime constant such things don't happen, however isn't this a pretty bad idea?

Would it be bad if I just fix it to 1/60 and I just cap fps at 60, there will be a problem if fps go under 60 though.

 

P.S. Btw first I was wondering where this error accumulation was coming from(in the beginning I thought I messed up something with the equations), I actually understood that I should put a constant deltaTime to fix the problem while trying to formulate the question.

 

And btw, am I missing something or should it be:

TranslateEntity in\obj,(in\vx+world\gravityx/2*deltaTime)*deltaTime*world\speedScale,(in\vy+world\gravityy/2*deltaTime)*deltaTime*world\speedScale,(in\vz+world\gravityz/2*deltaTime)*deltaTime*world\speedScale

	in\vx = in\vx + world\gravityx*deltaTime
	in\vy = in\vy + world\gravityy*deltaTime
	in\vz = in\vz + world\gravityz*deltaTime

Rather than the way I wrote it. I mean S = v0*t+1/2*a*t*t and not S = v0*t+a*t*t

Problem is I get an error accumulation with the equation that should be right...


template functions and STL list.sort

31 May 2013 - 12:12 PM

Hi!

With C++ I've currently gotten to the part  where I am learning different STL containers. I've been playing around with lists but I get an error, I just want to know why do I get an error when I do this:

template <typename T>
bool sortDescending(const T& in1, const T& in2)
{
    return in1>in2;
}

 

and then I just try to sort some list with it:

myList.sort(sortDescending);

 

If I change my sort function it's all fine:

bool sortDescending(const int& in1, const int& in2)
{
    return in1>in2;
}

 

 

Can anybody explain why exactly C++ forbids me from writing a more generic sort function?


else if question

22 May 2013 - 04:04 AM

First of all, I am reading a book on C++ - there was this one example in it:

#include <iostream>
using namespace std;

class Date
{
private:
	int day, month, year;
public:
	Date(int inputDay, int inputMonth, int inputYear)
		: day(inputDay), month(inputMonth), year(inputYear){}


	bool operator== (const Date& input)
	{
		if( (day == input.day) && (month == input.month) && (year == input.year) )
			return true;
		else
			return false;
	}

	bool operator< (const Date& input)
	{
		if(year < input.year)
			return true;
		else if(month < input.month)
			return true;
		else if(day < input.day)
			return true;
		else 
			return false;
	}

	bool operator<= (const Date& input)
	{
		if(this->operator== (input))
			return true;
		else 
			return this->operator< (input);
	}

};

int main()
{
	Date d1(19 , 4, 2013);
	Date d2(19, 5, 2012);
	cout << (d1<d2) << endl;

	cin.ignore();
	return 0;
}

 

 

Btw it's me that changed the main a little - just to show what a ridiculous output I get.

Is it me or is the inequality operator coded wrong?

	bool operator< (const Date& input)
	{
		if(year < input.year)
			return true;
		else if(month < input.month)
			return true;
		else if(day < input.day)
			return true;
		else 
			return false;
	}

 

If it were me I'd code it like this:

	bool operator< (const Date& input)
	{
		if(year < input.year)
			return true;
		else if(year > input.year)
			return false;
		else //when year == input.year
		{
			if(month < input.month)
				return true;
			else if(month > input.month)
				return false;
			else //when month == input.month
			{
				if(day < input.day)
					return true;
				else 
					return false;
			}
		}
	}

 

Is it the book's example that is wrong or is it me missing something crucial?


Super slow compiling?

13 May 2013 - 03:32 PM

Hi!

Since a few days ago I've been experiencing some extreme slow compile on Visual C++. I think that even when I try to play a music file with vlc it starts extremely slow - but it could be just me. The games I have start up normally though and I'm not experiencing any fps drop... I've noticed that if I restart the PC the compile time goes back to normal. Any advice to what may be causing this and how to fix it?

 

P.S. The ram usage is under 50% when that happens and the CPU may be even at 0% and at the same time I am waiting inexplicably long for some simple thing like properties showing up... Seems like Windows is "slow" and only at doing some tasks. My HDD works fine IMO as copying has a normal speed. The last things I did before this happened was install some custom windows themes, installed skype and copied around 100Gb from my external HDD - btw I still have around 500Gb free space (all of these could have just coincided with that).


Recursive functions - code critique?

30 April 2013 - 03:16 AM

Hi!

I am just learning recursive functions so I was wondering if you can give me any advice - here's my code:

#include <iostream>
#include <vector>
using namespace std;

int GetFibNumber(int);
//Recursive function
void ArrayFib(int);
void PresentArray();
vector<int> FibArray;

int main()
{
	int FibIndex = 0;
	cout << "Enter an index for the Fibonacci number you want: ";
	cin >> FibIndex;

	cout << "Here's the number at that FibIndex: " << GetFibNumber(FibIndex) << endl;

	cout << "Enter the index up to where you want the Fibonacci numbers: ";
	cin >> FibIndex;
	cout << "Here're the first " << FibIndex+1 << " Fibonacci numbers: " << endl;
	ArrayFib(FibIndex);
	PresentArray();

	cin.sync();
	cin.ignore();

	return 0;
}

int GetFibNumber(int index)
{
	if(index < 2)
		return index;
	else
		return GetFibNumber(index-2) + GetFibNumber(index-1);
}

void ArrayFib(int index)
{
	if(index == 0)
	{
		FibArray.push_back(0);
		return;
	}

	if(index == 1)
	{
		FibArray.push_back(0);
		FibArray.push_back(1);
		return;
	}

	if(index >= 2)
	{
		//Recursivity
		ArrayFib(index-1);
		FibArray.push_back(FibArray[index-2]+FibArray[index-1]);
                return;
	}

}

void PresentArray()
{
	for(int i = 0; i<FibArray.size()-1; i++)
	{
		cout << FibArray[i] << ", ";
	}

	if(FibArray.size() >= 1)
		cout << FibArray[FibArray.size()-1] << " The End!" << endl;

	return;
}

 

Are there any glaring mistakes? Anything you would recommend about this? (Btw I know I named my function and array kinda bad)


PARTNERS