Jump to content
  • Advertisement

Archived

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

adamxiii

This makes no sense!

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

This works: particle.life -= 0.01f * FrameInterval; but this doesn''t? particle.life -= particle.lifeRate * FrameInterval; lifeRate is just a float in the particle struct that is set to 0.01f and FrameInterval is the amount of time between frames. anyone know why this isn''t working for me? Heres the entire class if that helps anyone...
#ifndef CPARTICLE_H
#define CPARTICLE_H

#include "Globals.h"

#include "CFrameRate.h"

// Helper function to stuff a FLOAT into a DWORD argument

inline DWORD FtoDW( FLOAT f ) { return *((DWORD*)&f); }

class CParticle : public CObject
{
public:

	CParticle(string n);
	~CParticle();

	HRESULT Create( int num, CFrameRate &fps, string textureName );
	void FreeMemory();

	HRESULT Update();
	void Render();

private:
	LPDIRECT3DVERTEXBUFFER9 pVB;

	LPDIRECT3DTEXTURE9 pTexture;

	CFrameRate *pFPS;

	struct Particle
	{
		D3DXVECTOR3 position;
		DWORD color;

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


		D3DXVECTOR3 velocity;
		float life;
		float lifeRate;
	};

	DWORD D3DFVF_Particle;

	Particle *particles;
	int numParticles;

	void InitializeParticle( Particle &p );
	void UpdateParticle( Particle &p );
};

#endif
#include "CParticle.h"

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

//	Constructor

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

CParticle::CParticle(string n)
{
	//-----------------------

	//	Object Data


	name = n;
	type = PARTICLE;

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


	pVB = NULL;
	pTexture = NULL;

	particles = NULL;
	numParticles = 0;

	D3DFVF_Particle = D3DFVF_XYZ | D3DFVF_DIFFUSE;
}

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

//	Destructor

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

CParticle::~CParticle()
{
	FreeMemory();
}

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

//	FreeMemory

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

void CParticle::FreeMemory()
{
	SAFE_RELEASE(pVB);
	SAFE_RELEASE(pTexture);
}

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

// Create

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

HRESULT CParticle::Create( int num, CFrameRate &fps, string textureName )
{
	numParticles = num;
	pFPS = &fps;

	if( FAILED( D3DXCreateTextureFromFile( d3dDevice, textureName.c_str(), &pTexture ) ) )
		return E_FAIL;

	particles = new Particle[numParticles];

	// Create the vertex buffer.

    if( FAILED( d3dDevice->CreateVertexBuffer( numParticles*sizeof(Particle),
												 D3DUSAGE_POINTS | D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
												 D3DFVF_Particle,
                                                 D3DPOOL_DEFAULT, &pVB, NULL ) ) )
    {
        return E_FAIL;
    }

	return S_OK;
}

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

//	Update

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

HRESULT CParticle::Update()
{
    // Fill the vertex buffer.

    if( FAILED( pVB->Lock( 0, numParticles*sizeof(Particle), (void**)&particles, D3DLOCK_DISCARD ) ) )
        return E_FAIL;

	for(int i=0; i<numParticles; i++)
	{
		if( particles[i].life < 0 )
			InitializeParticle( particles[i] );
		else
			UpdateParticle( particles[i] );
	}

    pVB->Unlock();

	return S_OK;
}

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

//	InitializeParticle

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

void CParticle::InitializeParticle( Particle &p )
{
	p.position.x = 0;
	p.position.y = 0;
	p.position.z = 0;

	p.color = 0xFFFFFFFF;

	p.life = rand() % 1000 / 10000.0f;
	p.lifeRate = 0.01f;

	p.velocity.x = ( rand() % 1000 / 1000.0f ) - 0.5f;
	p.velocity.y = ( rand() % 1000 / 1000.0f ) - 0.5f;
	p.velocity.z = ( rand() % 1000 / 1000.0f ) - 0.5f;
}

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

//	UpdateParticle

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

void CParticle::UpdateParticle( Particle &p )
{
	p.position += p.velocity * pFPS->GetFrameInterval();

	// This makes no sense !!!!!!!

	p.life -= p.lifeRate * pFPS->GetFrameInterval();
}

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

//	Render

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

void CParticle::Render()
{
	if( SUCCEEDED( Update() ) ) {

		d3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
		d3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
		d3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

		d3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );
		
		// Set the render states for using point sprites

		d3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
		d3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE,  TRUE );
		d3dDevice->SetRenderState( D3DRS_POINTSIZE,     FtoDW(0.25f) );
		d3dDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(0.00f) );
		d3dDevice->SetRenderState( D3DRS_POINTSCALE_A,  FtoDW(0.00f) );
		d3dDevice->SetRenderState( D3DRS_POINTSCALE_B,  FtoDW(0.00f) );
		d3dDevice->SetRenderState( D3DRS_POINTSCALE_C,  FtoDW(1.00f) );

		d3dDevice->SetTexture( 0, pTexture );

		// Render

		d3dDevice->SetStreamSource( 0, pVB, 0, sizeof(Particle) );
		d3dDevice->SetFVF( D3DFVF_Particle );
		d3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, numParticles );

		// Reset render states


		d3dDevice->SetTexture(0,NULL);

		d3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, FALSE );
		d3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE,  FALSE );

		d3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
		d3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
	}
}

Share this post


Link to post
Share on other sites
Advertisement
In this line:

if( particles[ i ].life < 0 )
InitializeParticle( particles[ i ] );


particles[ i ].life is not initialised until the call to InitializeParticle, so InitialiseParticle is probably not being called.

I strongly suggest initialising the particles in Create:


particles = new Particle[numParticles];
for(int i=0; i<numParticles; i++)
{
InitializeParticle( particles[i] );
}


This way any calls made before the first call to update should be safe.


[edited by - Safely Anonymous on August 18, 2003 6:41:42 PM]

Share this post


Link to post
Share on other sites
I tried initializing all the particles in my create function but that didnt fix my problem, even though it was stupid of me not to do that anyway.

I changed the class around so lifeRate is now a member of the class and not of the particle struct and everything is working fine now.

I still have no idea why the previous way wasn''t working though...

Share this post


Link to post
Share on other sites
quote:
Original post by adamxiii

This works:
particle.life -= 0.01f * FrameInterval;

but this doesn't?
particle.life -= particle.lifeRate * FrameInterval;

lifeRate is just a float in the particle struct that is set to
0.01f and FrameInterval is the amount of time between frames.

anyone know why this isn't working for me?



"fLife -= 0.01f * FrameInterval"

is not equal to
"fLife -= fLife * FrameInterval".

The first equation reduces fLife more of less at a steady pace.
The second equation will make fLife goes down to zero quickly
if fLife is greater than one.

Just my guess there.





[edited by - HaywireGuy on August 18, 2003 9:40:34 PM]

Share this post


Link to post
Share on other sites
life and lifeRate are two totally different variables so that wasnt the problem.

I was trying to decrease the life slowly until it reaches 0. lifeRate is the velocity at which it will decrease so it can be framerate independent.

Share this post


Link to post
Share on other sites
quote:
Original post by glassJAw
Set up a breakpoint at that line and make sure the variables have the expected values.

Just stressing that this is what you should do.

Share this post


Link to post
Share on other sites
frameInterval is a float... right?
Tell me it''s a float, if not, cast it.


Oooh, you found the horadric nuke!
Did you know, that by using a horadric nuke, you can blow up Diablo and solve all our problems!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!