Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

pointer synax

Started by daniel_i_l 0 replies 550+ views
Original Post
daniel_i_l
daniel_i_l
I just wrote a function with a lot of pointers and referances, could you guys check if I screwed something up: -the first two objects don't need to be checked, they're there just cause the last function that want checkes uses them

struct CollisionInfo{
	CollisionInfo(){
		PointOnBody1.Zero();
		 PointOnBody2.Zero();
		 Normal.Zero();
		 Body1 = NULL;
		 Body2 = NULL;
	}
CollisionInfo(Vector2D a, Vector2D b, Vector2D n, RigidBody *c, RigidBody *d)
		:PointOnBody1(a),
		 PointOnBody2(b),
		 Normal(n),
		 Body1(c),
		 Body2(d)
	{}
	//virtual ~CollisionInfo(){}
	//stores the collisions point of the body in world space
	Vector2D  PointOnBody1;
	Vector2D  PointOnBody2;

	//stores the normal of the collision
	Vector2D  Normal;

	//pointers to the bodies involved in the collision
	RigidBody *Body1;
	RigidBody *Body2;

	//an operator used for sorting the collisions by height
friend bool operator<(const CollisionInfo &Start, const CollisionInfo &End)
     {
     return Start.PointOnBody2.y < End.PointOnBody2.y;
     }
};

////////////////////////////////////
class Collision{
public:
	Collision(){}
	//virtual ~Collision(){}
	bool CollisionDetected(RigidBody &b1, RigidBody &b2);
    
	//this function gets the COR as input and resolves all
	//the collisions in the list with impulses
	void ResolveCollisions(double c);

	//this function goes through all the collisions and
	//applies friction => havn't gotten that to work yet
	void ApplyFriction();

	//this function gives all the collisions a speed that 
	//will get them out of the collision in n frames
	void PullOutOfCollisions(int n, double dt);

	//clears the list
	void ClearCollisions(){m_vCollisions.clear(); m_vMidPoints.clear();}
	int  NumberOfCollisions(){return m_vCollisions.size();}
void SortFromBottomToTop(){sort(m_vCollisions.begin(), m_vCollisions.end());}
void Shuffle(){random_shuffle(m_vCollisions.begin(), m_vCollisions.end());}
void CombineInformation(){m_vCollisions.insert(m_vCollisions.begin(), 
m_vMidPoints.begin(), m_vMidPoints.end());}
	vector<CollisionInfo&> GetCollisions(){return CollisionInfo;}
private:
	//gets the collision info out of a point and two bodies
void GenerateCollisionInfo(Vector2D &point, RigidBody *b1, RigidBody *b2, 
PointType type);
	//vector of collisions
	vector<CollisionInfo> CollisionInfo;
	vector<CollisionInfo> m_vMidPoints;
};
//////////////////////////////////
//CHECK PLEASE
/////////////////////////////////
void RigidBody::ActivateFriends(vector<RigidBody> &bodies)
{
	Collision Collision;
	for(unsigned int j=0; j<bodies.size(); ++j)
		{
		Collision.CollisionDetected((*this), Bodies[j]);
		}
	Vector2D TestHeight = m_MyState.Position.y;
	vector<CollisionInfo&> Collisions= Collision.GetCollisions();
	for(unsigned int i=0; i<Collisions.size(); ++i)
	{
	RigidBody *body = Collisions.Body1;
	if((body != this)&&(!body->m_Active))
	{
	if(Collisions.Body1->m_MyState.Position.y > TestHeight)
	{
	Collisions.Body1->m_Active = true;
	body->ActivateFriends(bodies);
	}
	}

	RigidBody *body = Collisions.Body2;
	if((body != this)&&(!body->m_Active))
	{
	if(Collisions.Body1->m_MyState.Position.y > TestHeight)
	{
	Collisions.Body1->m_Active = true;
	body->ActivateFriends(bodies);
	}
	}
	}
}

Is that last function ok? are there any good optimizations? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Driv3MeFar
Driv3MeFar
Rather than rellying on other people to tell you this information, it would behoove you to do some debugging. Does the code compile? Does it behave as expected? Does it slow down your program to the point that it really needs optimization?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.