I am adding PointLights that follow the entities in the game. Here is how the light is added:
PointLight light; protected void initLight() { light = new PointLight(); light.DiffuseColor = Color.OrangeRed.ToVector3(); light.Intensity = .5f; light.Radius = 2f; Random rand = new Random(); light.Name = "pointlight" + rand.Next(100).ToString(); sceneInterface.LightManager.Submit(light); }
On update I set the light's position:
light.Position = Position; //Position is the position of the Entity
When the entity is removed from the game (disposed), I remove the light like this:
protected void RemoveLight() { light.Enabled = false; sceneInterface.LightManager.Remove(light); light = null; }
This works fine; however, lights also seem to be added in the center of the scene (0, 0, 0). Those lights do not disappear. Am I managing the lights incorrectly?
Hi khayman,
Is the code located in its own class, or inside the Game (or some other singleton) class? If the code is located in a singleton class like the Game class, then each call to InitLight() will replace the variable "light" with a new instance. This will cause any previous light to stop moving at the last updated location (as your code no longer references it).
Also make sure to set the light's ObjectType to Dynamic if the light moves frequently. This allows you to simply move the light without concern for the scenegraph. Static lights and objects are lower overhead, but require calling [manager].Move(obj) after moving.
[edit] Btw: we have new code on the way that helps with this type of coding pattern. Instead of keeping track of light and object references you can simply tie behaviors / event handlers to them.
As an example you could do something like this:
protected void InitLight() { PointLight light = new PointLight(); ... light.Name = <parent entity name>; light.UpdateEvent += MoveObjectWithNamedParentBehavior; ... sceneInterface.LightManager.Submit(light); } private void MoveObjectWithNamedParentBehavior(IMoveableObject obj, GameTime time) { ISceneObject parent; if(sceneInterface.ObjectManager.Find(obj.Name, out parent)) obj.Position = parent.World.Translation; }
This will handle any number of lights, objects, avatars, ... without having to track their references.
Let me know if this helps!
Follow me on Twitter – development and personal tweetsAwesome XNA Videos – Lighting, Rendering, and game videos
Thanks for the info, you were right. The light code was inside the entity's class; however, all my entities had DrawableGameComponent.Initialize() being called twice. I never noticed because the initialize method did not add anything to the game (until the lights). Thanks for finding a much more serious bug in the game!