Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (Quad, TipmyPip, degenerate_762, AndrewAMD, Nymphodora), 997 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Getting the closest point on a line and return the normal #456613
12/01/15 09:39
12/01/15 09:39
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline OP
Expert
alibaba  Offline OP
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
Hi guys,

lets say I have a line and a point. I need to get the closest point on the line to that point and also the normal on that point.
How would you achieve it? I have thought on some ways, but they are pretty dirty and slow.

You can interpret the situation as a "path" and a close "entity" in a level.


Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: Getting the closest point on a line and return the normal [Re: alibaba] #456614
12/01/15 09:43
12/01/15 09:43
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
You could use the following function

Code:
var get_line_segment_point_dist(VECTOR* vec1, VECTOR* vec2, VECTOR* point, VECTOR* vresult, var* fac)
{
	var dist;
	double loci;
	double dot1,dot2;
	VECTOR dir1,dir2;
	
	vec_diff(dir1,vec2,vec1);
	if(vec_length(dir1) < 1)
	{
		if(vresult) vec_set(vresult,dir1);	
		if(fac) *fac = 0.5;
		dist = (vec_dist(dir1,point));
		return dist;
	}
	vec_diff(dir2,point,vec1);
	dot1 = dir1.x*(double)dir1.x+(double)(dir1.y*(double)dir1.y)+(double)(dir1.z*(double)dir1.z);
	if(dot1 > -0.001 && dot1 < 0.001) return -1; 
	dot2 = dir2.x*(double)dir1.x+(double)(dir2.y*(double)dir1.y)+(double)(dir2.z*(double)dir1.z);
	loci = dot2/dot1;
	if(loci < 0) loci = 0;
	if(loci > 1) loci = 1;
	
	vec_scale(dir1,loci);
	vec_add(dir1,vec1);
	dist = vec_dist(dir1,point);	
	if(vresult) vec_set(vresult,dir1);	
	if(fac) *fac = loci;
	
	return dist;
}



and use

vec_diff(normal,point,vresult);
vec_normalize(normal,1);

to get the desired normal - assuming I got your issue correctly.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Getting the closest point on a line and return the normal [Re: Superku] #456622
12/01/15 14:52
12/01/15 14:52
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline OP
Expert
alibaba  Offline OP
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
That's exactly what I wanted to achieve, thanks a bunch! laugh


Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: Getting the closest point on a line and return the normal [Re: alibaba] #456625
12/01/15 17:16
12/01/15 17:16

M
Malice
Unregistered
Malice
Unregistered
M



@Superku - Nice!


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1