XNA Game Development Forums
2012/05/18 06:46:30 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Array Question  (Read 3351 times)
LucianX
Jr. Member
**
Offline Offline

Posts: 66



View Profile WWW
« on: 2007/03/18 00:56:19 »

        protected BoundingSphere[] updateBounds(BoundingSphere[] a, Vector3 real)
        {
            foreach (BoundingSphere l in a)
            {
                l.Center = real;
            }
            return a;
        }

im trying to cycle through all the spheres in the model.. and set the bounding sphere's center to the coordinate of the actual object. Since I don't know the size of the array untill runtime.. im trying to tdo this which would work.. however, i get an error.

Error: Cannot modify members of 'l' because it is a 'foreach iteration variable

Whats the method around this? Im trying to set values in each index of the array but the index is unknown because im going through all the indexes in the array and the max size isnt always known.

I didn't need arrays for a while and im kinda rusty in that area now. Any suggestions? The above code is just a method.. the other methods work alright and it creates the spheres already, i just have to set them to the objects movements with this method.

thx
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #1 on: 2007/03/18 13:17:52 »

Simple:

Code:
for(int i = 0; i < a.Length; i++){
    a[i].Center = real;
}

Looks like you haven't been coding for too long, as methods like these are fairly standard ways of doing things.

using the for loop is also a lot faster than a foreach (we were shown this by another member of the forums somewhere around here)
Logged
LucianX
Jr. Member
**
Offline Offline

Posts: 66



View Profile WWW
« Reply #2 on: 2007/04/06 23:09:19 »

What im trying to do is create a jagged or a 2 dimensional arrays.

In my content processor i have a getmeshvertices method that will get the vertices that make up all meshes in a model. I'm trying to store the first value of what object I am looping through to get the vertices and store it in the second part of the array. That way, in code I can read the first index of the array, and then read the second index which contains the vertex information. That way I can read the 1st mesh and then it's coordinates, and the same for the 2nd mesh and it's coordinates which would be in the 2nd place too.

With that array, i can check for collision with an object at a time without checking for all the coordinates at once, for this turns up false commands when i put in gravity.

Whats the best way to go about this?
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/04/07 14:17:46 »

I think you want something like:

Code:
            object[][] VertColl = new object[NoOfMeshes][];

            for (int msh = 0; msh < VertColl.Length; msh++)
            {
                VertColl[msh] = new object[NoOfVertsInMesh];
                for (int vrt = 0; vrt < VertColl[msh].Length; vrt++)
                {
                    VertColl[msh][vrt] = msh.ToString() + " - " + vrt.ToString();
                }
            }

Naturally you wont put msh.ToString() + " - " + vrt.ToString(); into your target element rather the data you want to store there. Be nice to see how you are getting the vert data from the mesh, any chance you can share this code?
Logged
LucianX
Jr. Member
**
Offline Offline

Posts: 66



View Profile WWW
« Reply #4 on: 2007/04/07 15:01:24 »

that would work. however, I don't know how long the array will be in terms of array.Length.  when i call the getVertices method I pass in 1 object and it gets the vertex information of that object and stores it in a list. Instead of that, I want it to store that meshes vertex information in an array and that array will be the 0th index of the array that contains it.

Array: [ 0 ] has Vector3 of that one object [1][2] etc.
Array: [1] will have the next object and contain its own vector3 array.

So when i test for collision, I can read Array[0] and its vector3   arrays     then read Array[1] and it's contents. So I can read each objects vertex formation at a time. Because I want to test each objects collision rather than the whole model at once because when I applied gravity that way, it would slowly sink into the level and fall through because sometimes it would return true and sometimes false because of all the vertex coordinates and the if statements I was using.

I have another question though, how can I properly test collisions with a spheres center (has radius too) with all the coordinates of an object?

I was using Sphere.Center.X - Vertices.X > Sphere.Radius  && the same for Y and Z. So if all 3 are true, then it would be a collision.

I think I might be missing a certan part. I have the collide with box and sphere then check for vertices already but I need more accurate tests.





Also, here's the code you wanted to look at.


Code:
        private List<Vector3> GetAllVerticies(MeshContent mesh)
        {
            for (int g = 0; g < mesh.Geometry.Count; g++)
            {

                GeometryContent geometry = mesh.Geometry[g];

                for (int ind = 0; ind < geometry.Indices.Count; ind++)
                {

                    // Transforms all of my verticies to local space.

                    Vector3 position = Vector3.Transform(geometry.Vertices.Positions[geometry.Indices[ind]], mesh.AbsoluteTransform);

                    MeshVerts.Add(position);

                }
            }
            return MeshVerts;
        }

Thats the method in the ContentProcessor that gets vertices and stores it in a list. But I wan't it to store it differently.. (Described above)

Hope this helps you out as well. Thanks in advance for the help Smiley
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #5 on: 2007/04/07 15:15:22 »

Thanks for the code  Cheesy

I see you are using a variable called MeshVerts and it is not defined in the method, I take it this is a global, if so you don't have to return it from the method.

Also, it's not an array you are after rather a collection, which you are already using with your List object. Also, if you did use arrays you do know the upper bounds they are mesh.Geometry.Count and geometry.Indices.Count.

Right, you are using a List object, try this.

Code:
        private List<List<Vector3>> GetAllVerticies(MeshContent mesh)
        {
            List<List<Vector3>> MeshVerts = new List<List<Vector3>>(mesh.Geometry.Count);
            for (int g = 0; g < mesh.Geometry.Count; g++)
            {
                GeometryContent geometry = mesh.Geometry[g];

                MeshVerts[g] = new List<Vector3>(geometry.Indices.Count);
                for (int ind = 0; ind < geometry.Indices.Count; ind++)
                {
                    // Transforms all of my verticies to local space.
                    Vector3 position = Vector3.Transform(geometry.Vertices.Positions[geometry.Indices[ind]], mesh.AbsoluteTransform);

                    MeshVerts[g].Add(position);
                }
            }
            return MeshVerts;
        }

I have never tried this as I normally use an ArrayList of ArrayLists, but this is probably better as it is typed. Hope this works.

I am now going to try and get vertex collision in my engine,  Tongue  I feel like a brain leech! All I do is nick other peoples ideas  Undecided
Logged
LucianX
Jr. Member
**
Offline Offline

Posts: 66



View Profile WWW
« Reply #6 on: 2007/04/07 16:20:36 »

It's giving me an index out of range on                 MeshVerts[g] = new List<Vector3>(Geometry.Indices.Count);


Instead of declaring the list<list> in the method, how would i code it so it is globaly? that way i can send it to the Process method and store it to the .tag.

What I really want to do is create an array with an array inside of it. the first index would be an array of all the coordinates of that mesh. You can see that the method only does 1 mesh at a time. How can I create a global array that can do this so it can be called in the process method? I cant seem to get all the syntax in order.


and do you have a method that can check against coordinates? Or is the if statement i typed above good enough?
« Last Edit: 2007/04/08 01:58:36 by LucianX » Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #7 on: 2007/04/08 05:11:07 »

OK, I got myself confused then, no need to set the bounds of a collection  Tongue

Try this I have just given it a go and it loads the vertex data up (I believe), where in your pipeline processor are you calling the method?

First declare MeshVerts at the top of your class, same place we declared the BoundingBox list, like this:
Code:
        List<List<Vector3>> MeshVerts;

Also declare the object to place in Tag, I have setup an object array to do this, 0 holds bounding box data, 1 is animation data (I will put the source up for this at some point) and 2 the new vertex position data:
Code:
        object[] ModelData = new object[3];

So the method now looks like this:
Code:
        private void GetAllVerticies(MeshContent mesh)
        {
            MeshVerts = new List<List<Vector3>>();
            for (int g = 0; g < mesh.Geometry.Count; g++)
            {
                GeometryContent geometry = mesh.Geometry[g];
                List<Vector3> temp = new List<Vector3>();
                for (int ind = 0; ind < geometry.Indices.Count; ind++)
                {
                    // Transforms all of my verticies to local space.
                    Vector3 position = Vector3.Transform(geometry.Vertices.Positions[geometry.Indices[ind]], mesh.AbsoluteTransform);
                    temp.Add(position);                   
                }
                MeshVerts.Add(temp);
            }
        }

At the moment, as I don't know where you are calling it from I have put this in the CheckNode like this:
Code:
                if (o is MeshContent)
                {
                    // Get VertData
                    GetAllVerticies((MeshContent)o);
                   ... rest of method ...

And in my Process method I bind the data to my object array like this:
Code:
            ModelData[0] = boxes;
            ModelData[1] = animationdata;
            ModelData[2] = MeshVerts;

            basemodel.Tag = ModelData;

Now to get the MeshVerts data back when in the engine I am using this code:
Code:
            if (myModel.Tag != null)
            {
                List<List<Vector3>> vertData = (List<List<Vector3>>)((object[])myModel.Tag)[2];
            }

The VertData object looks like the screen shot provided for the skulloc.x model in debug.

Have not done any vertex collision yet so I will have a think about how to go about doing it.

[attachment deleted by admin]
Logged
LucianX
Jr. Member
**
Offline Offline

Posts: 66



View Profile WWW
« Reply #8 on: 2007/04/08 10:34:40 »

Awesome, thanks.

It seems to only be getting the vertices for the whole model I made and not get a list for each mesh in it. If I can do vertex collision ok with the whole model loaded, then it wouldn't really matter.
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #9 on: 2007/04/08 12:42:46 »

This may be due to the redefenition of MesVerst each time it enters the method.

Remove this line in the method:
Code:
    MeshVerts = new List<List<Vector3>>();

And alter the declaration of the field to:
Code:
    List<List<Vector3>> MeshVerts = new List<List<Vector3>>();
Logged
LucianX
Jr. Member
**
Offline Offline

Posts: 66



View Profile WWW
« Reply #10 on: 2007/04/10 19:46:29 »

Do I need to convert the Jagged list to an actual array or can I check for collision with the list?
.
I would like to copy it to an array with the same layout the list has. Only problem.. I"ve never done it before. Can you post how I can copy the <List>List<Vector3>> to a Vertices[][]  array?  Thanks.

And did you get vertex working correctly for your engine? Ill let you know if I figure out the best vertex check solution. But untill then, I need to iterate through one object at a time and was never taught that Undecided

thx man.
« Last Edit: 2007/04/12 12:39:47 by LucianX » Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #11 on: 2007/04/13 04:51:19 »

All you have to do is take a look at my object[][] example and the List example, I am sure you can get your List into a Vector3[][] array if you have a good look at how they work.

Something like this:
Code:
Vector3 arr[][] = new Vector3[List.Count][];

for(int el=0;el < List.Count; el++)
    for(int itm=0;itm < List[el].Count;itm++)
            arr[el][itm] = (Vector3)List[el][itm];

Have not tested this, bit busy at the moment, but it should look something like this.
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Question about Getting 2D screen coordinates General Discussion DaphydTheBard 6 2462 Last post 2007/02/27 03:23:36
by DaphydTheBard
Question: Texturing Terrain General Discussion Ashe 1 2337 Last post 2007/03/17 10:28:36
by DaphydTheBard
Question about getting rotation angles from Quaternions... General Discussion DaphydTheBard 7 3132 Last post 2007/03/20 07:27:20
by DaphydTheBard
Tutorial 3 help... new question i believe Hazy Mind XNA Engine hansonc 9 3126 Last post 2007/05/15 01:32:01
by BackwardsBoxers
Question about loading textures General Discussion nicknz 2 1957 Last post 2007/04/23 05:36:17
by EclipsE
tutorial 3 sprite question Hazy Mind 3D Engine precious roy 2 2728 Last post 2009/10/18 08:53:11
by Wekbaite73
FPS question General Discussion XNASorcerer 2 1468 Last post 2007/04/28 15:23:19
by XNASorcerer
HMObject design question Hazy Mind XNA Engine jdowling 1 1266 Last post 2007/05/11 23:05:38
by Tiago
Thanks And A question Hazy Mind XNA Engine zachaller 3 1567 Last post 2007/06/19 03:36:22
by Nemo Krad
Question About Packing Maps into one file? General Discussion jaypaul 0 1560 Last post 2009/06/03 05:12:47
by jaypaul
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 2.289 seconds with 19 queries.