get distance of a vector

Post your scripting questions / solutions here

Moderator: Moderators

Post Reply
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

get distance of a vector

Post by ViPER »

I have a coord and a point of origin. I want to print the distance of each plane in order to duplicate the coord. how do you get the distance and account for neg coords calculated with positive coords?

EDIT: I think I got it. Thx
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

ooops, no i don't. :(

Ok let me try again.

I have a point of origin (that is different from map to map) But i wish to duplicate a second point that is relative to the first (in every map) If i had the distance x1 to x2, y1 to y2 and z1 to z2 i believe this is the answer.

how can I extract those distances and account for possible negative and positive cases?

does that make sense?
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

Um, perhaps abs (x2-x1) ?
Image
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

hmmm. not sure, what's abs do?

here's more detail. what i want to do is take a set of coords like...
(level.newcoords = ( x y z))

get the distance for each plane from the level.newcoords to the reference origin like $center.origin

then fill a new var like level.savedpointer (the distances of x,y and z from $center.origin to level.newcoords) This is how I hope to save a coordinant that is relative to $center.origin

the problem is (for example), when calculating the distance of $center.origin (x) to level.newcoord (x) where one is a positive and the other is negative, the result is off because I am not sure how to manipulate the coords so they are adding the total distance rather then (in some cases) adding a negative to a positive resulting in an incorrect distance.

how do I swap (in the event of) these cases so they handle the calcs properly?

also, im just treating each var as a whole, where im not sure how to get just x from $center.origin. which is what i need to do to do this.
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post by Rookie One.pl »

What planes do you mean?

Sorry, dude, but that's at most high school-level math.

The distance between any given points in any Euclidean geometry space (including the plain, old 3-dimensional space) is a square root of the sum of the squares of the differences of their components; e.g. for points P1(x1, y1, z1) and P2(x2, y2, z2), the distance between them is sqrt((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2).

The sign of the coordinates does not hinder distance calculations at all. If you have -2 and 5, the distance between them is 7, and you can easily calculate that as abs(-2 - 5), where abs is a function that returns the absolute value of a number (i.e. throws the minus away, if present). But that's not even high school, that's elementary school math.

Vector math in MoHAA scripting has been made extremely easy with nice operators. The distance between two points can be calculated like this:

local.dist = vector_length (local.p1 - local.p2)
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

I was in school many years before you were even born and my memory isn't so good. Hell, when you were in Elementary School learning this stuff, I had already retired. :wink:

im referring to coordinate planes. if I have the distance for each plane (from x to x , y to y and z to z) I can recreate the same coordinants of b where a is $center.origin.

will this do that? looks like you just get a single distance for point to point.

local.dist = vector_length (local.p1 - local.p2)

i try it thanks
Last edited by ViPER on Tue Mar 30, 2010 1:42 am, edited 4 times in total.
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

as I suspected that returns the distance from a to b. I want the distance from a to b for each plane x,y and z.

ALSO....

abs not compatible with vector types.... How do I get each coord (x y and z) out of local.origin so i can mess with them?
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

erm. I'll have another shot.

Code: Select all

local.vec = local.p1 - local.p2
local.dist_x = abs local.vec[0]
local.dist_y = abs local.vec[1]
local.dist_z = abs local.vec[2]
*boom* ? :)
Image
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

Yea man!! that works.

something wrong with my math tho. lol the result always lands in 1 quadrent. if the points origin is in any of the four quadrants the result always lands in +x +y quadrant.

the solution was to move the center origin to the corner. :?

this should work for me tho :) unless you have an idea what i might be doing wrong?


woohoo thanks!
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post by Rookie One.pl »

Eck, I still have no idea what you're after. :? But if it works, I'm happy for you. :)
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

lol glad it works :).
ViPER wrote:something wrong with my math tho. lol the result always lands in 1 quadrent. if the points origin is in any of the four quadrants the result always lands in +x +y quadrant.
I thought that was what you wanted. If you prefer -x and -y too, try it without the 'abs' :)
Image
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

jv_map wrote:lol glad it works :).
ViPER wrote:something wrong with my math tho. lol the result always lands in 1 quadrent. if the points origin is in any of the four quadrants the result always lands in +x +y quadrant.
I thought that was what you wanted. If you prefer -x and -y too, try it without the 'abs' :)
Thanks JV,

Yea man!!! that works. im saving each measure in a seperate cvar to get from map to map. will a cvar cary an array or is there a better way to do this?

here is what I am doing.

I want to save a game...... there are 3 maps finished ( and 2 more wip) and you can print a game to logfile and past it to script (that I intend to use for a chess tutorial and a set of chess puzzles) or save it to the server to finish your game later or move it to a different map. :)

Image
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post by Rookie One.pl »

Ah, so you just needed to decompose a vector into the separate scalar components! Heh. :)

On a side note, I believe it's possible to store a vector as a cvar (it will be implicitly cast to a string) and then have the string interpreted as a vector again on the next map.
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

yea, smart@55, what you said! :p

I'll give that a try with the vector conversion stuff. thanks.
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

Well this is not my cup of tea but using some examples from Elgans global strings script I came up with these.

Code: Select all

Leftint local.string:
	local.pos = 10
	local.pos--
	local.left = ""
	local.new = ""
	for(local.i = 0; local.i <=  local.pos; local.i++)
	{
		local.left = local.string[local.i]

		if( local.left == "(" ){
			local.new = ""}
			else{
			local.new += local.string[local.i]}
	}
end local.new

Code: Select all

Midint local.string:
	local.pos = 22
	local.start = "0"
	local.pos--
	local.left = ""
	local.new = ""
	for(local.i = 8; local.i <=  local.pos; local.i++)
	{
		local.left = local.string[local.i]

		if( local.start == "1" )
			local.new += local.string[local.i]

		if( local.left == "," )
			local.start = "1"
	}
end local.new

Code: Select all

Rightint local.string:
	local.pos = 32
	local.start = "0"
	local.pos--
	local.left = ""
	local.new = ""
	for(local.i = 16; local.i <=  local.pos; local.i++)
	{
		local.left = local.string[local.i]

		if( local.start == "start" )
			local.new += local.string[local.i]

		if( local.left == "," )
			local.start = "start"
	}
end local.new

it seems to work.... but It all seems a bit sloppy and inefficient. Any suggestions to improve this ? or perhaps over 90 cvars is just as good as 30 cvars and running these 3 threads each 30 times?

thoughts?
Post Reply