Accessing Vector Angle Data From a JS Program

Hello there,

I am new to Rhino and wanted to know if my application is something that can be done with this software.

I am working on a 2D game that runs using javascript. What I want to do is create a 2D vector field in Rhino and then give my game script access to the vector field data so that objects within the game move according to that field. In this case, vector magnitude does not matter and I am only concerned with vector angle when given a known coordinate. The in-game coordinate system is a square grid that is 40,000 units x 40,000 units.

My idea was to create the vector field in Rhino, export vector angle and coordinate data in txt format and use this as a discrete reference for vector angle values. Can Rhino export vector location and angle data? Can it do this with 1.6 B coordinates? Is there a better way to do it so I am not dealing with so much data such as referencing a function of the field?

@dale

Hi @Eric18,

Calculating the angle between two vectors is a widely known computation. I quick Google search should lead you to several JavaScript implementations.

As for Rhino, sure, it’s possible to perform these calculations. Rhino will not write the text file for you. But writing data to text files is pretty simple. In Grasshopper, you could do this with a Python or C# script.

Note, a vector does not have a location, just magnitude (length) and direction.

– Dale

Thanks for taking a look.

I will play around with it today and see what i can come up with.

To expand on what Dale mentioned, once you have the vector field data as text in GH and formatted how you want, you can use File IO to export to .CSV and read that into your video game environment.

You can also right click on a text panel in GH and choose “Stream Contents” to stream the text to a file path location.

However, depending on what your use case is there may be better ways to do it. Perhaps you create some vector field data in GH initially and then save pre-defined results or “steps” in said data iterations and save that to a file so that you can run your JS program independent of Grasshopper needing to be ran and within your JS program you can apply randomness, stepping, etc. so that it feels unique each time.

Thanks. Great info.

Played around with it today. I am shocked how quickly I was able to draw the field, use python within GH and get the data I needed.

1 Like

If it’s something you’re willing to share Id love to see your JS program /game when you get around to it

Yea sure I will share once it get it working.

Its not as “advanced” as you might think as my program is actually a chrome extension that adds new features to an existing game but the result should be pretty neat given the endless possibilities and ease of use that Rhino offers.

I did this one by programming a spiraling vector field directly into my code. I ended up here because I wanted to make more complex patterns that would be very difficult to do mathematically.

(please don’t mention the name of the game in the forums here as I’d like to avoid players of the game being able to search for me)

1 Like

Very cool thanks for sharing that! And yes no worries, I won’t post anywhere about it. Just wanted to see, cool stuff!