Particles stars?

Hi guys,
I’m not too confident in Blender so bare with me!
I’m building a space game with the BGE, this is what I need to do:

I need to create stars which my main character can fly past, is it possible to use a particle system to randomly place particles on the screen and keep them still?

Would an answer be creating an emitter then pause the animation towards the end so I have all the particles I need? Is there a better way of doing it?

Thanks in advance
Will

Hi SolarLune,

This looks really good! I’ve had a look at your videos and I think you’re running Blender 2.5 is that correct? I am using 2.49b, will your plugin still work?
I’m gonna have a proper look into it tomorrow, looks great.
Cheers
Will

Thanks. Yes, you are correct that I’m using 2.57. While the blend may not work in 2.49b, the plugin should if you change the 2.57 import commands to 2.49b (GameLogic instead of bge.logic), assuming you set up the objects to run the same way. It’s fairly simple - the Emitter just runs the Emitter script to emit the particles, and a particle calculation script to display and update the particles correctly.

I could release a version that uses GameLogic, as apparently that is present both in 2.49b and 2.57.

Hi SolarLune,

Thanks, releasing a version which uses GameLogic would be great!
I have a few questions, sorry I haven’t done anything like this before so bare with me!

I have tried changing the python scripts myself to run GameLogic instead of bge.logic and now I’m getting a few errors. I have a feeling this might be because I am running an earlier version of python? When I start up blender it says it’s compiled with Python version 2.6.2. The errors which I am getting are the following:

Python script error from controller “Python#CONTR#2”:
Traceback (most recent call last):
File “C:\ file path \Particle.py”, line 248, in Fire
obj.scaling = [obj[‘scale’].x + obj[‘sf’], obj[‘scale’].y + obj[‘sf’], obj[‘scale’].z + obj[‘sf’]]
KeyError: ‘value = gameOb[key]: KX_GameObject, key “scale” does not exist’

Python script error from controller “Emit#CONTR#1”:
Traceback (most recent call last):
File “C:\ file path \XEmitter.py”, line 388, in Emitter
apos = obj.position.copy() # Makes a copy of the smoke box’s position and edit it
AttributeError: ‘list’ object has no attribute ‘copy’

Thanks for your help

Ah, this is before the vector classes changed… In 2.5x, certain position, scaling, etc. variables are actually Vector-type variables, not common Python lists. Unfortunately, I would have to rewrite some of the code to port it over to 2.49… If you’re interested in 2.57, check it out - maybe you’ll like it.

Ah right, that’s a shame.
I think I have to stick to using 2.49, because I’m building something for an immersive dome, and I need the dome correction functionality built into 2.49. I think I remember someone saying it hasn’t been put into 2.5x.

Thanks for your help anyway SolarLune, looks like I’m going to have to figure out a different route for this one.

Would it be easy to write a script which randomly placed a number of spheres on the scene? I am a programmer, but I’m finding it incredibly hard to learn Python at the moment, there is a lot of confusion over different versions. Would you be able to point me in the right direction?

Also how do I run a python script at the beginning, I can attach it to an always sensor, but I only want it to run once.

Thanks,
Will

I once read a DirectX book, where the author used a number of (virtual*) Spheres(27) that surrounded the camera and used the position as a seed to init a random number generator. This generator was used to create the actual stars(sprites). The SourceCode is written in C++, I don’t know if you are interested in it.

*= he didn’t actually use Spheres, he just used it as a model to explain the idea.

Hm that sounds like an interesting way of doing it, generative star creation. I will have a think of how to do this in Python. Do you still have the book lying around?

You can run a script once by attaching a Python controller to an Always sensor that has no True or False pulse settings.

I will just post the SourceCode. I have translated the comments, so hopefully they will be useful.


// Renders the "Starfield"
tbResult CGame::RenderStarfield(float fTime)
{
	int			iSeed;
	tbVector3	vCameraPos;


	// Round Camera position up towards the next "1000er" (convert to int and back).
	vCameraPos.x = (float)((int)(m_vCameraPos.x / 1000.0f) * 1000);
	vCameraPos.y = (float)((int)(m_vCameraPos.y / 1000.0f) * 1000);
	vCameraPos.z = (float)((int)(m_vCameraPos.z / 1000.0f) * 1000);

	// Iterate through the 27 Spheres that surround the camera  
	for(float x = vCameraPos.x - 1000.0f; x <= vCameraPos.x + 1000.0f; x += 1000.0f)
	{
		for(float y = vCameraPos.y - 1000.0f; y <= vCameraPos.y + 1000.0f; y += 1000.0f)
		{
			for(float z = vCameraPos.z - 1000.0f; z <= vCameraPos.z + 1000.0f; z += 1000.0f)
			{
				// Is the Sphere visible? (culling)
				if(tbSphereVisible(tbVector3(x, y, z), 1500.0f, m_aViewFrustum))
				{
					// init the random number generator
					// through an arbitrary formula
					iSeed = (int)(x + 10.0f * y + 100.0f * z);
					tbSRand(iSeed);

					// create 100 stars in form of sprites
					// the author calls a method to create the sprite
                                        // the first parameter is the type of the sprite, the rest is pretty clear 
					for(int i = 0; i < 100; i++)
					{
						m_pSprites->AddSprite(tbIntRandom(2, 4),
											  tbVector3(x, y, z) + tbVector3Random() * tbFloatRandom(0.0f, 1500.0f),
											  tbColor(1.0f, 1.0f, 1.0f, tbFloatRandom(0.25f, 0.9f)) - tbColorRandom(0.0f) * 0.25f,
											  tbVector2(tbFloatRandom(1.0f, 5.0f)),
											  tbFloatRandom(0.0f, TB_PI),
											  TRUE);
					}
				}
			}
		}
	}

	// not necessary, author resets the seed to the current frame time
	tbSRand((int)(g_pGalactica->m_fTime * 10000.0f) % 10000);

	return TB_OK;
}

To recreate this in python you can use the random module. There are also several resources on sprites, like this one.

EDIT: Maybe the pastebin version is slightly better readable.

@SolarLune Ah that makes sense, stupid I didn’t think of that actually! Thanks.

@C++User thanks for posting the source code, I will look into it tomorrow, hope I will get it working!

Cheers for your help guys, it’s really appreciated :smiley: