# [XNA 4.0] BoundingSphere moves when it should not

This topic is 2265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey

I have a XNA 4.0 project in which I load a file that contains type and coordinates of items I need to draw to the screen. Also I need to check if one particular type (the only movable one) is passing in front or trough other items. This is the code I use to load the configuration:

 if (ks.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.L)) { this.GraphicsDevice.Clear(Color.CornflowerBlue); Otvaranje.ShowDialog(); try { using (StreamReader sr = new StreamReader(Otvaranje.FileName)) { String linija; while ((linija = sr.ReadLine()) != null) { red = linija.Split(','); model = red[0]; x = red[1]; y = red[2]; z = red[3]; elementi.Add(Convert.ToInt32(model)); podatci.Add(new Vector3(Convert.ToSingle(x), Convert.ToSingle(y), Convert.ToSingle(z))); sfere.Add(new BoundingSphere(new Vector3(Convert.ToSingle(x), Convert.ToSingle(y), Convert.ToSingle(z)), 1f)); } } } catch (Exception ex) { Window.Title = ex.ToString(); } } 

The "Otvaranje" is an OpenFileDialog object, "elementi" is a List<Int32> (determines the type of item that would be drawn), podatci is a List<Vector3> (determines the location where the items will be drawn) and sfere is a List<BoundingSphere>. Now I solved the picking algorithm (checking for ray and bounding sphere intersection) and it works fine, but the collision detection does not. I noticed, while using picking, that BoundingSphere's move even though the objects that they correspond to do not. The movable object is drawn to the world1 Matrix, and the static objects are drawn into the world2 Matrix (world1 and world2 have the same values, I just separated them so that the static elements would not move when the movable one does). The problem is that when I move the item I want, all boundingSpheres move accordingly. How can I move only the boundingSphere that corresponds to that particular item, and leave the rest where they are?

##### Share on other sites
Are world1 and world2 are different after changing the positition of the active boundingsphere? If you render the inactive ones with world2 then the world2 has to be actually the Matrix.Identity-Matrix and world1 has to be Matrix.Identity with the position translation.

##### Share on other sites
Hm, I'm not sure what you mean, the world1 and world2 are identity matrices in the beginning, and I draw the items (movable and static) with this method:

 private void DrawModels(Model model, Matrix world, Matrix view, Matrix projection, Color boja) { foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect be in mesh.Effects) { be.EnableDefaultLighting(); be.DiffuseColor = boja.ToVector3(); be.SpecularColor = boja.ToVector3(); if (model == kutija) { be.World = (mesh.ParentBone.Transform * Matrix.CreateTranslation(pozicija)) * worldKutija; } else { be.World = (mesh.ParentBone.Transform * Matrix.CreateTranslation(pozicija)) * world; } be.View = view; be.Projection = projection; } mesh.Draw(); } } 

"pozicija" is a Vector3 variable that gets its value from the file. When I move the box, it is done (in this moment) with picking like this:

 if (sfere[0].Intersects(KontrolnaZraka) != null && Microsoft.Xna.Framework.Input.ButtonState.Pressed == Mouse.GetState().LeftButton) { worldKutija = Matrix.CreateTranslation(new Vector3(0.01f, 0f, 0f)) * worldKutija; } 

"sfere[0]" is an element of the list I mentioned in my question, and "KontrolnaZraka" is a Ray class object. So when the box is clicked it moves per 0.01f by X-axis as you see. This is how I detected that the BoundingSpheres move. I altered the code so that the box moves when I click on any BoundingSphere, not just the one corresponding to the box (the 0 index in "sfere" list). I noticed that the box moves, and then stops as if the condition in the if clause is false. Then I click a few inches to the right and the box starts moving again.

##### Share on other sites
I dont know how to work with basiceffects, But actually you have to apply a pass of an effect.
Maybe you can use something like be.passes[0].apply() after assigning the parameters to be. It seems, that every boundingsphere is using the same basiceffect-parameters.

##### Share on other sites
Can you suggest some alternative that would have the same result? Like I said the only thing left for me to do is to detect the passage of the movable item in front/trough the static items.

##### Share on other sites
 be.EnableDefaultLighting(); be.DiffuseColor = boja.ToVector3(); be.SpecularColor = boja.ToVector3(); if (model == kutija) { be.World = (mesh.ParentBone.Transform * Matrix.CreateTranslation(pozicija)) * worldKutija; } else { be.World = (mesh.ParentBone.Transform * Matrix.CreateTranslation(pozicija)) * world; } be.View = view; be.Projection = projection; be.Passes[0].apply(); //new line 

Whats wrong with it? Does that not work?

##### Share on other sites
No, the same thing keeps happening

##### Share on other sites
could you upload the project? Its hard to find bugs without the whole sourcecode.

##### Share on other sites
Here it is, but you might have some problems with it since it is in Croatian, and there is still some code that I don't use at the moment. If you have any questions please ask. Also, since this is a college project, please do not share it anywhere else without telling me about it.

Removed attachment by user request
-MJP

##### Share on other sites
Where is the level-file?

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 9
• 17
• 20
• ### Forum Statistics

• Total Topics
632600
• Total Posts
3007338

×