Welcome to Open Carnage

A resource for gamers and technology enthusiasts, with unique means of rewarding content creation and support. Have a wander to see why we're worth the time!

Sign in to follow this  
Followers 0
Nickster5000

Get Distance Utility Function

I figured since I couldn't find a command to do this in SAPP's documentation, and I needed it heavily in the mod I am working on, I thought it'd be a nice utility function to release:

 

function getDistance(Player1, Player2) --returns the distance between two players
	Player1X = get_var(Player1, "$x")
	Player1Y = get_var(Player1, "$y")
	Player1Z = get_var(Player1, "$z")
	Player2X = get_var(Player2, "$x")
	Player2Y = get_var(Player2, "$y")
	Player2Z = get_var(Player2, "$z")
	local diffX = Player1X - Player2X --x1-x2
	diffX = diffX * diffX--((x1-x2)^2)
	local diffY = Player1Y - Player2Y
	diffY = diffY * diffY
	local diffZ = Player1Z - Player2Z
	diffZ = diffZ * diffZ
	local sum = diffX + diffY + diffZ
	return math.sqrt(sum)
end

 

Share this post


Link to post
Share on other sites

I'd honestly do this very differently:

-- My first impulse on how to do it:
function GetPlayerDistance(index1, index2)
	p1 = get_dynamic_player(index1)
	p2 = get_dynamic_player(index2)
	p1x, p1y, p1z = read_vector3d(p1+0x5C)
	p2x, p2y, p2z = read_vector3d(p2+0x5C)
	
	return math.sqrt( (p2x-p1x)^2 + (p2y-p1y)^2 + (p2z-p1z)^2 )
end

-- Less lines:
function GetPlayerDistance(index1, index2)
	p1x, p1y, p1z = read_vector3d(get_dynamic_player(index1)+0x5C)
	p2x, p2y, p2z = read_vector3d(get_dynamic_player(index2)+0x5C)
	
	return math.sqrt( (p2x-p1x)^2 + (p2y-p1y)^2 + (p2z-p1z)^2 )
end

-- The technically correct way (Also works in chimera without the bottom snippet):
function GetPlayerDistance(index1, index2)
	p1 = get_dynamic_player(index1)
	p2 = get_dynamic_player(index2)
	
	return math.sqrt((read_float(p2+0x5C  ) - read_float(p1+0x5C  ))^2
	                +(read_float(p2+0x5C+4) - read_float(p1+0x5C+4))^2
	                +(read_float(p2+0x5C+8) - read_float(p1+0x5C+8))^2)
end


-- If you are using the chimera api read_vector3d() doesn't exist.
-- And this piece makes it available if you're not on sapp
if sapp_version == nil then
	function read_vector3d(address)
		return read_f32(address), read_f32(address+4), read_f32(address+8)
	end
end

 

Nickster5000 and Tucker933 like this

Share this post


Link to post
Share on other sites

I agree. I try to stay away from doing too much memory access outside of the SAPP functions, but that implementation is much more concise. I changed it to your implementation if that's okay with you. 

Share this post


Link to post
Share on other sites
5 hours ago, SBBMichelle said:

I'd honestly do this very differently:


Stuff i removed cause it would bloat this comment.

 


You may also want to check that the player's object exists before trying to read their x,y,z (that is if you use the player object.)  Alternatively you could use the static player address:

local x,y,z = read_vector3d(get_player(index1) + 0xF8)

Which wont cause a crash or error even if the player is dead, it will just return their last position where they were alive.
 

Edited by Devieth
Nickster5000 likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0
  • Recently Browsing   0 members

    No registered users viewing this page.