Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by ina · Jan 20, 2012 at 03:58 PM · arrayvector3datastructhashtable

Array or Data Structure with Vector3 as keys

Is it possible to set up an array or data structure of some kind where the keys are vector3's?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Jan 20, 2012 at 05:14 PM 0
Share

How would that be helpful? They're three floating point numbers!

avatar image ina · Jan 20, 2012 at 05:34 PM 0
Share

Hmm... suppose you have just one gameobject in each discrete coordinate, and you want to reference the one at a particular coordinate...

avatar image jahroy · Jan 20, 2012 at 05:56 PM 0
Share

It seems obvious why this would be helpful. This is why people use hashtables and dictionaries all the time.

avatar image Jessy · Jan 20, 2012 at 05:58 PM 0
Share

The coordinates are not "discrete" if they're represented by floating point numbers. You should describe the application for which you've come to this convoluted solution.

avatar image ina · Oct 08, 2013 at 07:40 AM 0
Share

what if your rounded the floats down to tenth

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by petrucio · Jul 17, 2015 at 07:15 AM

You can't use Vector3 as keys directly, since the hash will be generated from the object itself, not it's contents, which is what you want.

I've made an extension that returns a unique int from a vector. I'm my case I only need to know it's discrete integer values, and I assume they will never be larger than 1000. You can change it to suit your needs if needed, but the gist will be the same.

 //=========================================================================
 // Rounds coordinates to integers, and returns an int that can easily be hashed and used as a key
 // Assumes coordinates will never be greater than 1000 in any direction
 public static int HashableInt(this Vector3 vector)
 {
     int x = Mathf.RoundToInt(vector.x);
     int y = Mathf.RoundToInt(vector.y);
     int z = Mathf.RoundToInt(vector.z);
     return x * 1000 + z + y * 1000000;
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zamaroht · Jun 11, 2016 at 08:16 PM 0
Share

Also, this doesn't work if the objects are closer than 1 unity from each other. In any other case, it's a perfectly valid way to do it.

avatar image tanoshimi · Jun 11, 2016 at 08:21 PM 0
Share

And it will also go wrong with, say... x:1, y:0, z:-500

which produces the same hash as: x:0, y:0, z:500

avatar image Bunny83 · Jun 11, 2016 at 11:52 PM 2
Share

This answer is actually wrong. You can use a Vector3 directly as a key in a dictionary. The GetHashCode method of Vector3 looks like this:

 public override int GetHashCode()
 {
     return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
 }

Which is a quite strong hash. The hash for y and z are shifted 2 bits so they don't cancel each other out when they have the same value(s). Of course the Vector3 has to hold an exact value. The slightest error will result in a different hash. So "23.12345f" is not the same as 23.12346f.

The hash created in this answer is very weak it only works for a quite limited range. If you use a dictionary and you want to use a 3 component vector for whole numbers you should create your own struct and implement GetHashCode the same way Unity did for Vector3. I once created a "Vector3i" struct for exactly this purpose. It's basically the same struct but it uses ints ins$$anonymous$$d of floats.

avatar image
0

Answer by jahroy · Jan 20, 2012 at 06:03 PM

You could use a hashtable or a dictionary:

 var objectArray : GameObject [];
     
 /* example using a hashtable */
 
 var vectorHash : Hashtable = new Hashtable();
 
 /* create hashtable to lookup gameobjects by their position */
 
 for ( var thisObject : GameObject in objectArray ) {
 
     var theVect : Vector3 = thisObject.transorm.position;
 
     vectorHash[theVect] = thisObject;
 }
 
 
 /* example using a dictionary */
 
 var vectorTable : Dictionary.<Vector3, GameObject> =
     new Dictionary.<Vector3, GameObject> ();
 
 
 for ( var thisObject : GameObject in objectArray ) {
 
     var theVect : Vector3 = thisObject.transorm.position;
 
     vectorTable[theVect] = thisObject;
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Oct 08, 2013 at 07:41 AM 0
Share

it seems that .Contains does not work with Vector3's as keys

avatar image petrucio · Jul 17, 2015 at 07:17 AM 0
Share

No, this won't work. See my answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lists and Structs instead of Arrays? 4 Answers

c# : Accessing Struct variables inside an Array 1 Answer

Linking array of sets of vector3's together 1 Answer

Max and min Vector3 in array 1 Answer

Access 2 of the 3 components of Vector3 (Vector3.xy?) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges