OasisGames

I am trying to make projectiles that my object can shoot, but am having trouble adding new projectiles to my List<projectile> object.

My projectile code (so far) looks like this:
public class projectile
{
float rotation;
float rotationAlpha;
Vector3 position;
Vector3 velocity;
float life = 0f;
public void Update()
{
position += velocity * 1000f;
life += .1f;
}
public projectile(Vector3 position, Vector3 velocity, float rotation, float rotationAlpha)
{
this.position = position;
this.velocity = velocity;
this.rotation = rotation;
this.rotationAlpha = rotationAlpha;
this.life = 0f;
}
public void Create(Vector3 position, Vector3 velocity, float rotation, float rotationAlpha)
{
this.position = position;
this.velocity = velocity;
this.rotation = rotation;
this.rotationAlpha = rotationAlpha;
this.life = 0f;
}
public void Render(Model projec, Vector3 cameraPosition, Vector3 camLook, float aspectRatio)
{
Matrix[] transforms = new Matrix[projec.Bones.Count];
projec.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in projec.Meshes)
{
//This is where the mesh orientation is set, as well as our camera and projection
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationX(rotationAlpha) * Matrix.CreateRotationY(rotation)
* Matrix.CreateTranslation(position);
effect.View = Matrix.CreateLookAt(cameraPosition, camLook, Vector3.Up); //Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
aspectRatio, 1.0f, 1000000.0f);
}
//Draw the mesh, will use the effects set above.
mesh.Draw();
}
}


And I'm trying to add projectile objects to a list<projectile> ProjList.
I have tried ProjList.Add(new projectile(modelPosition, modelVelocityAdd, modelRotation, modelRotationAlpha); but this doesn't seem to work.

Ideas


Re: XNA Game Studio Express Creating projectiles, need help with list.

George Clingerman

When you say that it doesn't seem to work, is there a specific error that you are getting There's a bunch of things that could be going on, but it would help if there was a little bit more information about the behavior that you are getting.

I do similar code in some of my projects and adding objects to a list has always worked so knowing what error you are getting would be a great clue in troubleshooting this.





Re: XNA Game Studio Express Creating projectiles, need help with list.

OasisGames

ProjList.Add(new projectile(modelPosition, modelVelocityAdd, modelRotation, modelRotationAlpha));
Returns:
"Object reference not set to an instance of an object."




Re: XNA Game Studio Express Creating projectiles, need help with list.

George Clingerman

Do you instantiate your list object before you use it

List<projecticle> ProjList = new List<projectile>();

You won't be able to add items to the list before you do that.





Re: XNA Game Studio Express Creating projectiles, need help with list.

Gunston

George is right that this is the likely cause.

This kind of thing can be highlighted at compile time as a warning, for example:

Field 'ProjList' is never assigned to, and will always have its default value null YourFile.cs

You could have the compiler treat warnings the same way as errors (i.e. the build fails) by going to Project|Properties... and, under the Build tab, choose "All" in the "Treat warnings as errors" section.  At the same time, you also need to set the "Warning level" - the default is 4 which will catch unassigned variables.

Treating warnings as errors can be a real pain when you're in the early stages of coding a new project or when you're in the middle of a heavy refactoring session but when things stabilise, my advice would be to treat warnings as errors and set the warning level to 4.

Hope this helps.

Cheers.






Re: XNA Game Studio Express Creating projectiles, need help with list.

George Clingerman

Did you get this working or are you still having issues Just curious if I had guessed what was going on correctly or not.





Re: XNA Game Studio Express Creating projectiles, need help with list.

MattDavey

On a side note, it would be more efficient to store your projectiles in a LinkedList object, as iterating through them is faster....

//Old method (slow)
List<Projectile> _projectiles = new List<Projectile>();
//.....
foreach (Projectile projectile in _projectiles)
{
projectile.Update();
//etc...
}




Re: XNA Game Studio Express Creating projectiles, need help with list.

George Clingerman

Thanks for adding that Matt, that should be useful information for more than just the original question asker. I know that I wasn't aware of that.

Well, I think we got this answer pretty well covered. Nice job all!





Re: XNA Game Studio Express Creating projectiles, need help with list.

Gunston

Matt

First, I'm *not* suggesting what you're saying isn't the case.

I had a quick trawl of MSDN to find a relevant article (so I could add a link to the thread and get "closure") but couldn't find one. In my day job, I've done a lot of STL/C++ where the performance characteristics of container types are very clearly defined. I was searching for things like:

o(n) o(1) linkedlist list iterator performance

Appreciate if you could reference an article and I agree with George that this will help everyone.

Cheers.






Re: XNA Game Studio Express Creating projectiles, need help with list.

Shawn Hargreaves - MSFT

Something to be aware of when analyzing data structure performance, especially on platforms like Xbox that have in-order processors and hence are relatively slow at memory access compared to their computational performance, is that O-notation isn't really accurate compared to the real world. Describing one algorithm as O(n) while another is O(n*2) ignores the constant time factors, and in the real world where n is often quite small, those constant factors can easily end up being more significant than the n part!

In this case, ordered iteration through a linked list, array, or System.Collections.Generic.List object (which is just a wrapper around a dynamically resizable array) are all O(n). Advancing the iterator to the next object is a constant time operation.

The place where a linked list might be faster is if you need to add or remove entries in the middle of the list. That's a constant time operation for a linked list (assuming you already have an iterator to the insertion/removal point), but O(n) for an array.

BUT! When you look at the constant factors, linked lists loose out big time.

Advancing to the next entry in an array is easy. Increment the index, and you're done. Advancing to the next entry in a linked list looks equally easy: just look up the Next reference from the current instance. These operations look very different when you bring the data cache into the picture, though.

With an array, the next value is physically located immediately after the previous one. If you just did some work on that previous object, that means it got loaded into the cache, and chances are the memory immediately after it will already have been loaded too, so this will be primed and ready for you to go. Intel processors even have dedicated hardware for noticing when you are reading linearly through a large block of memory, and will automatically prime the cache even further ahead of the current line. Nice and fast.

With a linked list, the next object could be anywhere in memory, so it is quite unlikely to already be in the cache. This means that following that harmless looking Next reference will probably miss cache, cause a stall, and stop the CPU dead in the water.

Even if you are lucky and your list entries do happen to be contiguous in memory (which is actually rather more likely in managed code than in native, thanks to the way the garbage collector works), the linked list will still have additional overhead. Because you are storing many independently allocated objects as opposed to an array holding all of them, the memory manager needs to store some header data in front of each allocation. That isn't useful for your program, but still takes up space and increases the pressure on your data cache. Also, reference heavy structures like linked lists cause the garbage collector to do more work checking each of those links, where with an array it would only have to examine the whole thing in one go, so this can increase your collection latencies.

In other words, beware of linked lists. In the right context they can be a huge win, but for general use, an array (or a System.Collections.Generic.List) will usually be faster.