Really strange problem!! whats going on?
Dear all,
I have this really weird problem I just can''t figure out. All im trying to do is loop through some CParticle objects and initialise the attributes (such as starting position, velocity etc.) the code to loop through and initialise these attributes is shown below!
when the 3 lines which initialise the velcity (vel) are removed, then the next three lines to initialse the position (pos) work fine. And the function "view_Particle_Attributes()" (below) prints out expected results for the particles starting positions! i.e.
5.134112
-3.243244 etc
However, if I include the velocity initialising code (the 3 lines highlighted below), then the position (pos) initialisation code after doesnt work properly. From the function "view_Particle_Attributes()" results like the following are shown.
296485.937500
134222.109375
329004.123341 etc.
this shouldnt happen since the function "randomRange" should make sure they only come out in the range of -10 to 10. Its when i include the velocity init. code that this problem happens!
But why does initialising the velocity (vel) cause me not to be able to initialise the position (pos)? I just dont understand what is going on here? Any suggestions at all anyone?
Any help would be MUCH appreciated! I''ll post more code if it helps!
for (int i = 0; i < 10; i++)
{
CParticle *n = PartNew();
if (!n)
return;
### if these next 3 lines are commented out, everything is ok! #
// n->vel[0] = CRand::randomRange(1.0f, 10.0f);
// n->vel[1] = CRand::randomRange(1.0f, 10.0f);
// n->vel[2] = CRand::randomRange(1.0f, 10.0f);
// and these are initialised properly!
n->pos[0] += CRand::randomRange(-10.0f, 10.0f);
n->pos[1] += CRand::randomRange(-10.0f, 10.0f);
n->pos[2] += CRand::randomRange(-10.0f, 10.0f);
n->pold.Set(n->pos);
}
void CParticleSystem::view_Particle_Attributes()
{
for (CParticle *curr=pUse.next; curr!=&pUse curr=curr->next)
{
char temp[50];
sprintf(temp, "%f", curr->pos[0]);
MessageBox(NULL, temp, "Title", MB_OK);
}
}
#########################################################
#include "Vector.h"
class CParticle
{
public:
CParticle();
virtual ~CParticle();
CVector vel;
CVector pos;
CVector pold;
CParticle *next, *prev;
};
#########################################################
Hmmm... Interesting... More code would help... I''m sure the source of the problem is right there but it''s early in the morning and I really can''t see it right now...
Just to clarify, when you initialise your particles, you dont leave their pointers dangling do you? It looks like you allocate them on the heap but then you dont retain their pointers, and what is pUse?
-------- E y e .Scream Software --------
----------------------------------------
/-\
http://www.eyescream.cjb.net | * |
\-/
----------------------------------------
Just to clarify, when you initialise your particles, you dont leave their pointers dangling do you? It looks like you allocate them on the heap but then you dont retain their pointers, and what is pUse?
-------- E y e .Scream Software --------
----------------------------------------
/-\
http://www.eyescream.cjb.net | * |
\-/
----------------------------------------
hello,
im sure the initialisation of the particles is ok, because the position (pos) does initialise properly when the velocity (vel) is commented out.
pUse is a dummy link at the start of the single link list of the list of particles currently active (pAll). But im sure the problem isnt here.
Any suggestions, ill post more code if ive missed something!
Thanks for the previous post! and ANY help is appreciated!
CParticle* CParticleSystem:
artNew(void)
{
if (!pFree)
return 0;
CParticle *n = pFree;
pFree = pFree->next;
n->prev = &pUse
n->next = pUse.next;
pUse.next->prev = n;
pUse.next = n;
return n;
}
then the class CParticleSystem has
class CParticleSystem
.....
Protected:
CParticle *pAll; // single link
CParticle pUse; // double link
CParticle *pFree; // single link
}
im sure the initialisation of the particles is ok, because the position (pos) does initialise properly when the velocity (vel) is commented out.
pUse is a dummy link at the start of the single link list of the list of particles currently active (pAll). But im sure the problem isnt here.
Any suggestions, ill post more code if ive missed something!
Thanks for the previous post! and ANY help is appreciated!
CParticle* CParticleSystem:

{
if (!pFree)
return 0;
CParticle *n = pFree;
pFree = pFree->next;
n->prev = &pUse
n->next = pUse.next;
pUse.next->prev = n;
pUse.next = n;
return n;
}
then the class CParticleSystem has
class CParticleSystem
.....
Protected:
CParticle *pAll; // single link
CParticle pUse; // double link
CParticle *pFree; // single link
}
*sigh* Sorry I can''t see the problem myself... It appears to be a problem with your linked lists, that seems definite... You could try making temporary changes to your code to make sure of that.
You could try using vector (from stl) if that doesn''t work.
-------- E y e .Scream Software --------
----------------------------------------
/-\
http://www.eyescream.cjb.net | * |
\-/
----------------------------------------
You could try using vector (from stl) if that doesn''t work.
-------- E y e .Scream Software --------
----------------------------------------
/-\
http://www.eyescream.cjb.net | * |
\-/
----------------------------------------
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement