Jump to content
  • Advertisement
Sign in to follow this  
LostTime77

Updating a class member

This topic is 3794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again everybody! I have a question with regards to updating a class member. This may seem like a game question at first light, but it's really not. Anyhow.... Basically, I have a vector array holding a bunch of class objects called masses, or actually circle objects. Each mass has its own mass, velocity vector (2d so only x, y properties), acceleration vector, position vector, and a set of vertex objects that each has its own position in the 2d world (also a vector2d). Now...., as code is to follow do not get spooked at how simple my vector class is. I tried to delete all the operator overloading and methods so all I have left are the x, y, z members and a constructor, to illustrate my problem. Each frame of the engine update, I use a force generator which basically just adds a force to a netForce data member (also a vector2d) each frame so that acceleration, velocity, and position can be solved based on Euler physics methods. In my force generator class I iterate through all the circle objects and use the AddForce method, which the circle objects have. This AddForce method takes one parameter which is a vector2d and passes by reference (to illustrate). Now.. finally the actual problem comes when I try to manipulate the vector data members in the circle class. No matter what I do....., the vector forces will not update. I even went as far as to manipulate the x and y properties of the actual vectors themselves. No dice.. As one can see, in the code I try to add 1 to the y property of one of the forces each frame in the AddForce method. For some reason when I printf the value of this property each update, the force that I updated does not retain its original value. This leads me to believe that somehow my vector is being constructed each frame of the update. I do not see how though. It makes sense because when I construct a vector, the x, y, and z members of a vector are set to 0.0f; This would seem true... but move on to clue 2. When I go into another method in the circle class (the Integrate method), the vector retains its value each frame, however the AddForce method(which gets called every frame update), only updates the vector once even though I use the += operator. This shows when I print the value of the y member of the vector in the Integrate method. Clue 3. Remember that gravity force vector that is a parameter to the AddForce method? Well.. I can update that just fine I can do gravity.y += 1 and then print the value each frame and it comes out exactly right. The force retains its y value like it should. Hopefully somebody out there can help me. Im probably missing something very obvious. Please tell me if im not being clear here because this is a very odd problem as one vector updates fine, but another vector of another class does not. I have tried many things, among them I tried making the netForce vector public in the Mass class. Final Notes - Sorry if the code is long or long winded. I am trying to develop a complex program here that will eventually become a physics engine. Hopefully you can just bear with it. I will post as many source files as I can so that the problem can be illustrated correctly. If there is anything commented out in the source code, its probably my Alloc console code which allows me to allocate a console to a win32 program and use printf in the source file I allocated it in. Finally - circle objects are just instances of the Mass class. Thanks!!!!! Source code to follow....... Vector2.h
//#ifndef H_VECTOR2
//#define H_VECTOR2

#include <math.h>

	class Vector2
	{
		public:
			float x, y, z;
			Vector2(void);
		private:
	};

//#endif

Vector2.cpp
#include "Vector2.h"


	Vector2::Vector2(void)
	{
		x = 0.0f;
		y = 0.0f;
		z = 0.0f;

		return;
	}

EngineHandler.cpp -> UpdateEngine gets called every frame to update all the mass objects in the 2d world. MH is the mass handler. This is a specific object that deals solely with updating the mass objects.
void EngineHandler::UpdateEngine(const long &duration,
		                             DirectXStructure &DXS)
	{
		MH.UpdateMasses();
		MH.IntegrateMasses(duration, DXS);

		return;
	}

MassHandler.cpp -> These are the two methods from mass handler that get called each frame.
	void MassHandler::IntegrateMasses(const long &duration,
		                              DirectXStructure &DXS)
	{
		for(int i = 0; i < masses.size(); i++)
		{
			masses.at(i).Integrate(duration, DXS);

			return;
		}
	}

	void MassHandler::UpdateMasses(void)
	{
		GG.UpdateForces(masses);

		return;
	}

GravityGenerator.cpp -> GG is the gravity force generator object. This object is used to add a force vector to each object.
	void GravityGenerator::UpdateForces(vector<Mass> masses)
	{
		for(int i = 0; i < masses.size(); i++)
		{
			//printf("%f  \n", gravity.y);
			masses.at(i).AddForce(gravity);
		}
	}

Masses.h -> Here is the header class file for the mass objects (circle objects)
#ifndef H_MASS
#define H_MASS

#include "Globals.h"
#include "Structures.h"

	class Mass
	{
		public:
			Mass() {}
			Mass(const float&,
				 const float&,
				 DirectXStructure&,
				 const float[],
				 const float[],
				 const int[]);
			void AddForce(Vector2&);
			void Integrate(const long&,
				           DirectXStructure&);
			Vector2 netForce;
		private:
			float inverseMass;
			Vector2 acceleration;
			Vector2 position;
			Vector2 velocity;
			Vertex2 verts[MASSPOLYCOUNT];
	};

#endif

Mass.cpp -> Here are the two methods that are used to update each circle object directly.
void Mass::AddForce(Vector2 &force)
	{
		//force = force + 1;
		//printf("%f \n ", netForce.y);
		//netForce.y = netForce.y + 1;
		//printf("%f  \n", netForce.y);
		//force.y += 1;
		//printf("%f \n", force.y);
		

		netForce.y += 1;
		printf("%f \n", netForce.y);

		return;
	}

	void Mass::Integrate(const long &duration,
		                 DirectXStructure &DXS)
	{
		//printf("%f  \n", netForce.y);
		//acceleration = netForce * inverseMass;
		//velocity += acceleration * duration;
		//position += velocity;

		//printf("%u  \n", duration);

		/*D3DXMatrixTranslation(&DXS.transform,
			                  position.x,
							  position.y,
							  position.z);

		DXS.DXD.D3DDevice->SetTransform(D3DTS_WORLD,
			                            &DXS.transform); */

		//acceleration.Clear();

		return;
	}

-----> netForce.y will NOT retain its value each frame even thought I use +=. LostTime [Edited by - LostTime77 on July 22, 2008 3:39:22 PM]

Share this post


Link to post
Share on other sites
Advertisement
Here:


void GravityGenerator::UpdateForces(vector<Mass> masses)


You are making a copy of the vector when you call this function. You want:


void GravityGenerator::UpdateForces(vector<Mass> &masses)


I would imagine. Hope this helps.

Share this post


Link to post
Share on other sites
Oh my god! I knew it was something as simple as that. I changed to pass by reference for the two vector objects and it worked perfectly! I thank you! I guess I just needed a new set of eyes :)

Thread is now dead :)

LostTime

Share this post


Link to post
Share on other sites
Quote:
Original post by LostTime77
Oh my god! I knew it was something as simple as that. I changed to pass by reference for the two vector objects and it worked perfectly! I thank you! I guess I just needed a new set of eyes :)


No probs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!