0 registered members (),
1,454
guests, and 8
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
vec_dist question
#407241
09/10/12 06:09
09/10/12 06:09
|
Joined: Apr 2008
Posts: 245
GameScore
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 245
|
i want try to find out wich entity is the closest to the mouse_pos3d i use a trace from mouse_pos3d like this
vec_add(vec_scale(vec_set(trace_target, mouse_dir3d), 1000), mouse_pos3d);
c_trace (mouse_pos3d,trace_target,SCAN_TEXTURE | USE_POLYGON | IGNORE_FLAG2);
vec_set(mouse_hit.x,hit.x);
and calculate the the dist
for(i=1;i<=ent_count; i++)
{
var distance=vec_dist(ent.x[i],mouse_hit.x);
}
with this i have the distance of all counted entitys but how can i calculate wich is the closest?
|
|
|
Re: vec_dist question
[Re: GameScore]
#407303
09/11/12 07:45
09/11/12 07:45
|
Joined: Dec 2003
Posts: 1,225 germany
gri
Serious User
|
Serious User
Joined: Dec 2003
Posts: 1,225
germany
|
so go through the array, compare the values and save the smallest one in a variable
whats the problem?
"Make a great game or kill it early" (Bruce Shelley, Ensemble Studios)
|
|
|
Re: vec_dist question
[Re: gri]
#407325
09/11/12 13:51
09/11/12 13:51
|
Joined: Apr 2008
Posts: 245
GameScore
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 245
|
can u help me please how i can compare the values? i really dont find a workingsolution
void test()
{
int i,t,p;
for(i=1 ; i<array_size ; i++)
{
t=i+1;
if(array[i] < array[t])
{
p = array[i];
}
if(array[i] > array[t])
{
p = array[t];
}
}
}
this only works with a array who have the size of 2
|
|
|
Re: vec_dist question
[Re: SchokoKeks]
#407330
09/11/12 14:33
09/11/12 14:33
|
Joined: Apr 2008
Posts: 245
GameScore
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 245
|
Jaaaaaaaaaaaaaaaah! thanks man! thats what i ws locking for i`m so thanfull u forgot a small thing in your example forgot the "="
for (i = 0; i <= array_size; i++)
i`m so happy now! thank you schokokes
|
|
|
Re: vec_dist question
[Re: GameScore]
#407332
09/11/12 14:51
09/11/12 14:51
|
Joined: Nov 2002
Posts: 913 Berlin, Germany
SchokoKeks
User
|
User
Joined: Nov 2002
Posts: 913
Berlin, Germany
|
Glad to help u forgot a small thing in your example forgot the "="
no, i did that on purpose. Remember that in most programming languages counting and arrays start at 0. so if you have an array with 2 entries (array_size=2), you access them with i=0 and i=1. If you use i=2, there will usually be no direct crash but you access memory that does not belong to the array! that is very bad, and if you change this memory you will get a crash somewhere totally unrelated and its extremely hard to figure out where this crash comes from. So be very carefull with array indices!
|
|
|
Re: vec_dist question
[Re: SchokoKeks]
#407335
09/11/12 16:18
09/11/12 16:18
|
Joined: Apr 2008
Posts: 245
GameScore
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 245
|
|
|
|
|