Archived

This topic is now archived and is closed to further replies.

Problem with loop

This topic is 5585 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

I've been looking at this for hours and I can't figure out what I'm doing wrong. Everything works fine until one of my objects goes off the screen. When the ToBeKilled flag is set to 1, the following code kills off the object like it's suppoed to, but then it kills of every object for the rest of the loop as well. Can anyone see what I'm doing wrong?
for ( int Num = 0; Num < g_nNumberOfGameObjects; Num++ )
{
	if ( m_pGameObject[Num]->ToBeKilled == 1 )
	{
		m_pGameObject[Num] = NULL;
		m_AddObject ( Num );
		m_pGameObject[Num]->m_Spawn ( );
	}
	m_pGameObject[Num]->m_Transform ();
        m_pGameObject[Num]->m_Render ();
}  
- m_pGameObject is an instance of a class to keep track of the game objects. - m_AddObject(Num) populates a game object with a mesh - m_Spawn() resets all variables to 0 and then picks new values. I've definitely isolated the problem to this loop, but can't figure out beyond that what I've done wrong. Using VC++.NET and DirectX 8.1. Thanks. Edit - From looking at the values, it appears that whatever values are present when the if staement gets triggered are then used for all the objects remaing in the loop. [edited by - JimboC on September 1, 2002 11:56:55 AM]

Share this post


Link to post
Share on other sites
quote:
Dunno if this is the whole problem, but if tobekilled is true, then you set the object pointer to null. After the if(), though, you call functions in an object that you just set to null.
I should've cleaned that up before I posted. While trying different things I've added the m_pGameObject[Num] = NULL; line to the loop and added m_pGameObject[Number] = new CObject3D; to the AddObject method. I know this will be slow, but I'm just trying to get it working right now. Didn't make any difference to the problem though. Should I post more of the code?


[edited by - JimboC on September 1, 2002 12:23:35 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
post code for your array object ... maybe there is something there.

Share this post


Link to post
Share on other sites
Here''s the code for the CObject3D Class. (m_Transform and m_Render are near the bottom):

  
//////////////////////////////////////////////////////////////////////

// CObject3D.cpp

//////////////////////////////////////////////////////////////////////


#include "Global.h"
#include "CObject3D.h"

//--------------------------------------------------------------------

// Constructor

//--------------------------------------------------------------------

CObject3D::CObject3D ( )
{
int ToBeKilled = 0;

// Position

float m_XLoc = 0.0f;
float m_YLoc = 0.0f;
float m_ZLoc = 0.0f;

// Rotation

float m_XRotation = 0.0f;
float m_YRotation = 0.0f;
float m_ZRotation = 0.0f;

// Temp Move Holder

float m_XMoveValue = 0.0f;
float m_YMoveValue = 0.0f;
float m_ZMoveValue = 0.0f;

// Total Movement per second

float m_XChange = 0.0f;
float m_YChange = 0.0f;
float m_ZChange = 0.0f;

// SetError ( "Constructor Done." );


}

//--------------------------------------------------------------------

// Destructor

//--------------------------------------------------------------------

CObject3D::~CObject3D()
{
if ( m_pMeshMaterials )
delete m_pMeshMaterials;

if(m_pMeshTextures != NULL)
{
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
if(m_pMeshTextures[i])
{
m_pMeshTextures[i]->Release ();
}
}
}

if ( m_pMeshTextures )
delete m_pMeshTextures;

if ( m_pMesh )
m_pMesh->Release ();
}

//--------------------------------------------------------------------

// CObject3D::m_LoadObject ( LPSTR pFilename, DWORD dwKeyColor )

//--------------------------------------------------------------------

void CObject3D::m_LoadObject ( LPSTR pFilename, DWORD dwKeyColor )
{
LPD3DXBUFFER pMaterialsBuffer = NULL;
if ( FAILED ( D3DXLoadMeshFromX ( pFilename, D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL,
&pMaterialsBuffer, &m_dwNumMaterials, &m_pMesh ) ) )
{
SetError ( "CObject3D: Mesh Failed to Load!" ) ;
return;
}

D3DXMATERIAL* matMaterials = (D3DXMATERIAL*)pMaterialsBuffer->GetBufferPointer();
m_pMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];

for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
m_pMeshMaterials[i] = matMaterials[i].MatD3D;
m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
if( FAILED ( D3DXCreateTextureFromFileEx( g_pd3dDevice, matMaterials[i].pTextureFilename, 0, 0, 0, 0,
D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, dwKeyColor, NULL, NULL, &m_pMeshTextures[i] ) ) )
{
m_pMeshTextures[i] = NULL;
}
}
pMaterialsBuffer->Release ();
D3DXComputeNormals(m_pMesh, NULL);
}

//--------------------------------------------------------------------

// CLaserTurret::m_Render ( )

//--------------------------------------------------------------------

void CObject3D::m_Render ( )
{
if( m_pMesh != NULL )
{
for( DWORD i = 0; i < m_dwNumMaterials; i++)
{
g_pd3dDevice->SetMaterial ( &m_pMeshMaterials[i] );
g_pd3dDevice->SetTexture ( 0, m_pMeshTextures[i] );
g_pd3dDevice->SetTextureStageState ( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );

m_pMesh->DrawSubset ( i );
}
}
return;
}


//--------------------------------------------------------------------

// CObject3Dt::m_Transform ( )

//--------------------------------------------------------------------

void CObject3D::m_Transform ( )
{
D3DXMATRIX matMove, matXRotate, matYRotate, matZRotate,
matRotTemp, matRotate, matObject;
ToBeKilled = 0;

if ( m_XRotation != 0.0f )
D3DXMatrixRotationX ( &matXRotate, timeGetTime()/ m_XRotation );

if ( m_YRotation != 0.0f )
D3DXMatrixRotationY ( &matYRotate, timeGetTime()/ m_YRotation );

if ( m_ZRotation != 0.0f )
D3DXMatrixRotationZ ( &matZRotate, timeGetTime()/ m_ZRotation );

D3DXMatrixMultiply ( &matRotTemp, &matXRotate, &matYRotate );
D3DXMatrixMultiply ( &matRotate, &matRotTemp, &matZRotate );

if ( g_fElapsedTime != 0 )
{
m_XMoveValue = ( m_XChange * g_fElapsedTime );
m_XLoc += m_XMoveValue;

m_ZMoveValue = ( m_ZChange * g_fElapsedTime );
m_ZLoc += m_ZMoveValue;
}
D3DXMatrixTranslation ( &matMove, m_XLoc, m_YLoc, m_ZLoc );
D3DXMatrixMultiply ( &matObject, &matRotate, &matMove );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matObject );

if ( (m_ZLoc < -50.0f) || (m_XLoc > 175.0f) || (m_XLoc < -175.0f) )
{
ToBeKilled = 1;
SetError ( "================= ToBeKilled =================" );
SetNumError ( &m_ZLoc );
SetNumError ( &m_XLoc );
}
else
{
ToBeKilled = 0;
}
}

//--------------------------------------------------------------------

// CObject3D::m_Spawn ( )

//--------------------------------------------------------------------

void CObject3D::m_Spawn ()
{
// Zero Object

ToBeKilled = 0;
m_XLoc = 0.0f;
m_YLoc = 0.0f;
m_ZLoc = 0.0f;
m_XRotation = 0.0f;
m_YRotation = 0.0f;
m_ZRotation = 0.0f;
m_XMoveValue = 0.0f;
m_YMoveValue = 0.0f;
m_ZMoveValue = 0.0f;
m_XChange = 0.0f;
m_YChange = 0.0f;
m_ZChange = 0.0f;

m_XLoc =(150.0f * (float)(rand()/(double)RAND_MAX) ) - ( 150.0f * (float)(rand()/(double)RAND_MAX) );
m_ZLoc = 350.0f;
m_XChange =(25.0f * (float)(rand()/(double)RAND_MAX) ) - ( 25.0f * (float)(rand()/(double)RAND_MAX) );
m_ZChange =-(10.0f * (float)(rand()/(double)RAND_MAX) + 15 );

float RateOfRotation = 0.0f;
RateOfRotation = ( 1000.0f * (float)(rand()/(double)RAND_MAX) ) + 500;
if ( (float)(rand() / (double)RAND_MAX ) < 0.5f )
RateOfRotation = -(RateOfRotation);
m_XRotation = RateOfRotation;

RateOfRotation = ( 1000.0f * (float)(rand()/(double)RAND_MAX) ) + 500;
if ( (float)(rand() / (double)RAND_MAX ) < 0.5f )
RateOfRotation = -(RateOfRotation);
m_YRotation = RateOfRotation;

RateOfRotation = ( 1000.0f * (float)(rand()/(double)RAND_MAX) ) + 500;
if ( (float)(rand() / (double)RAND_MAX ) < 0.5f )
RateOfRotation = -(RateOfRotation);
m_ZRotation = RateOfRotation;
}


Share this post


Link to post
Share on other sites
If all you are doing when killing an object is re-intializing it, all you should need is


  
for ( int Num = 0; Num < g_nNumberOfGameObjects; Num++ )
{
if ( m_pGameObject[Num]->ToBeKilled == 1 ) { m_pGameObject[Num]->m_Spawn();
}
m_pGameObject[Num]->m_Transform();
m_pGameObject[Num]->m_Render();
}

Share this post


Link to post
Share on other sites
quote:
Original post by Deyja
If all you are doing when killing an object is re-intializing it, all you should need is
I''m also picking a new type of meteor when the old one dies.

I did take out the line that picks a new meteor type and the loop problem went away. Unfortunately, now it''s only displaying the first couple of meteors that are initialized.

Methinks I need to look a little closer at what''s going on when I initialize a new meteor. Thanks for the suggestions so far, I''m going to try and figure it out for myself from here.

Share this post


Link to post
Share on other sites