running through list

Started by
2 comments, last by Zakwayda 19 years, 6 months ago
Hi, im making a list where I store my objects and run through them in order to calculate a few physics for them, here is the main source code

#include "ph1.h"
#include "timer.h"
#include "objeto.h"
#include "vector.h"

#include <iostream>

using namespace std;

int main()
{
	PH1 sim;
	Timer myTimer;
	myTimer.start();
	sim.AddObject(Objeto(5.0f, Vector(0,0,0), Vector(1,0,0), Vector(0,0,0)));
	sim.AddObject(Objeto(5.0f, Vector(0,0,0), Vector(1,2,0), Vector(0,0,0)));
	for(int i = 1; i < 3; i++)
	{
		sim.RunPh1(myTimer.ellapsed()); //run the simulation
		sim.cabeza.pos.toString(); //print the position information
	}
}

So the problem comes when i run RunPh1, because i think im not adding correctly the objects. here is ph1.cpp

#include "objeto.h"
#include "ph1.h"
#include <iostream>
PH1::PH1()
{
	cabeza.siguiente=NULL;
	count = 0;
}


void PH1::AddObject(Objeto nuevo)
{
	if(count == 0)
	{
		cabeza = nuevo;
		count = 1;
		std::cout << cabeza.siguiente << std::endl;
	}
	else
	{
		temporal = cabeza;
		while(temporal.siguiente != NULL)
		{
			if(temporal.siguiente != NULL) temporal = *temporal.siguiente;
		}
		temporal.siguiente = &nuevo;
		count++;
	}
}

void PH1::RunPh1(float dt)
{
	std::cout << "time: " << dt << std::endl;
	temporal = cabeza;
	do
	{
		temporal.vel+=(temporal.force / temporal.m) * dt;
		temporal.pos+= (temporal.vel * dt);
		if(temporal.siguiente != NULL) temporal = *temporal.siguiente;
		

	}
	while(temporal.siguiente != NULL);

}

I think my addobject method is not adding in new list items correctly, i know its a lot to go through it but if anyone takes their time i will be very grateful. NOTE: cabeza means head (of the list)
I have neurons in my butt!!!
Advertisement
Use a C++ std::list<Objeto> instead of your custom list class.

#include "timer.h"#include "objeto.h"#include "vector.h"#include <list>#include <iostream>// Typedef for a list of objectstypedef std::list<Objeto> objlist_t;// Runs a simulation step// Pass objlist by reference because we do modify the listvoid RunPh1(objlist_t& objlist, float dt){    std::cout << "time: " << dt << std::endl;    // Iterator, a kind of "pointer" into the list    objlist_t::iterator itor;    // Loop from the beginning of the list, until the end    // end() is *not* part of the list (think of it as a NULL marker)    // ++itor moves the iterator to the next element    for( itor = objlist.begin(); itor != objlist.end(); ++itor )    {        // Note that we are using pointer-like syntax        itor->vel += (itor->force / itor->m) * dt;        itor->pos += (itor->vel * dt);    }}int main(){    // The list of objects    objlist_t objlist;    Timer myTimer;    myTimer.start();    // push_back adds an element at the ... back of the list    objlist.push_back( Objeto(5.0f, Vector(0,0,0),                                    Vector(1,0,0),                                    Vector(0,0,0)) );    objlist.push_back( Objeto(5.0f, Vector(0,0,0),                                    Vector(1,2,0),                                    Vector(0,0,0)) );    for(int i = 1; i < 3; i++)    {        RunPh1(objlist, myTime.ellapsed()); // run the simulation        objlist.front().pos.toString();     //print the position information    }}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hi,

Ok, I don't know if I can help, but I am trying to sort through your code.

Well, you may be much smarter than me and using some approach that I'm not aware of :) Barring that, though, I think you are using objects where you really want to be using pointers. I'll rewrite some of your code to show you what I mean:

int main(){    PH1 sim;    Object o1(/* stuff */);    Object o2(/* stuff */);    sim.AddObject(&o1);    sim.AddObject(&o2);    // etc...}PH1::PH1() {head = NULL;}void PH1::AddObject(Object* o){    if (!head)    {        head = o;        head->next = NULL;    }    else    {        // Insert o at front of list (easier)        o->next = head;        head = o;    }}void PH1::RunPh1(float dt){    Object* o = head;    while (o)    {        o->vel += //etc...        o->pos += //etc...        o = o->next;    }}


That's off the top of my head, so I may have missed something. Also, there are some larger issues here about shared pointers and pointer safety. But that can wait until later.

Anyway, I hope that was of some help. Let us know if you have any more questions.
Oh, and Fruny's right. Although it'd probably be good to understand basic stuff like pointers well enough to write your own code for stuff like this, once you get more comfortable you will want to use the stl for these sorts of situations.

[Edit: Once you get to dealing with objects of any complexity, though, or objects that dynamically allocate memory, you'll want to use a vector<Object*> (or list or whatever) instead of a vector<Object>.]

This topic is closed to new replies.

Advertisement