Finding distances in three-dimensional space

Okay, math time. I’m coding a game in which the relative positions of things are very important. I need to be able to find the distance between any two arbitrary objects in three-dimensional space. I know how to do this in something like C++, but implementing it in I7 has proven to be a challenge. I’m also going to need the trig functions. And I’ll need to run these calculations many times per turn, so they’ll need to be efficient. Any suggestions?

EDIT: I’ve gotten it working, but the extension I used is Glulx-specific. I’d prefer to stay in Z-Code if possible, but if it isn’t, I can live with that.

I assume you’re using the Fixed Point Maths extension. I believe that the only reason that this extension requires Glulx only because numbers can be larger. If you’re able to work with smaller numbers, e.g. one or two decimal places, you could get numbers as large as 3276.8 or 327.68. Try making a new copy of Fixed Point Maths and remove the “(for Glulx only)” from the title, and also change the settings in Chapter 1, Section 2 of the extension to reflect the new precision. Here’s an example with 2-digit precision:

[code]Section 2 - Constants for precision

Precision is a number that varies. Precision is 2.
Divisor is a number that varies. Divisor is 100.
HalfDivisor is a number that varies. HalfDivisor is 49.
Pi is a real number that varies. Pi is 3.14.[/code]

There might be other things in the extension you’d have to change (I haven’t looked too closely), but I think that should do it.

–Erik

Oh, you’ll also need to change the definition of the real number type, e.g.:

[code]Section 1 - Defining a fixed point number

A real number is a kind of value. -999.99 specifies a real number with parts whole and fractional.[/code]

–Erik

Okay, I’ll try that, but I’d rather get it working with the unmodified extension first. That way, if your method doesn’t work for some reason, I’ll at least have it working under Glulx. (Not that I doubt that you know what you’re talking about: I just am always nervous about modifying other people’s code, because it’s never documented in a way that I can understand. (I am not saying that people don’t document stuff well, just that I can never understand it. (Also, nested parenthesis ftw.)))

OK. But I thought you said you already had it working under Glulx? There’s an edit to your original post that says that you do…

To make you rest easier, I guess I should point out that the changes I suggested aren’t changes to Michael’s code. They’re just changes to configuration variables he provided. (Of course, there might be other changes that are needed that I didn’t look deep enough–and I barely skimmed–to see.)

–Erik

I meant that I got the part I had gotten enough of it working to be sure that the extension would work for my purposes. There’s still a long way and a lot of trig to go.

I just did a brief test, and I think this would work under z-code, but you’d need to both make the changes I already suggested, and go through the extension and change all of the four-digit precision numbers (e.g. 1.0000) to whatever precision you might end up using (e.g. 1.0).

–Erik

Okay, thanks. I’m still going to do that last, though, but at least now I know that I can stop looking for other extensions. You’ve saved me a lot of wasted time.

Hey Biophysicist,

Just curious–did you get anywhere with this? Able to get it doing what you wanted?

–Erik

Like most of my projects, this one died because it wasn’t fun. But I did get it working.