Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Distance? (https://forums.graalonline.com/forums/showthread.php?t=134264735)

pig132 10-08-2011 03:49 AM

Distance?
 
Okay i'm going to start working on something and it requires a distance formula between a player and another player(or npc). When I mess around with one distances come up as negatives sometimes depending where the player is. I have ran into one a few times a while back but I can't seem to find it.

Any help would be appreciated, thank you ^^


EDIT: I did a quick mockup of what my end goal is to be (with players only. npc was just to test)
Pretty much the further a player is from another player the less alpha the player has. I just have no idea how I could accomplish this. GANIs? Add a weapon to everyone that has it?

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
setshape(13232);
  
setTimer(0.05);
}

function 
onTimeout() {
  
temp.np findnearestplayer(this.xthis.y);
  
temp.dx this.player.x;
  
temp.dy this.player.y;
  
this.chat temp.np ": " temp.dx "," temp.dy;
  
  if (
temp.dx && temp.dy <= 3) {
    
this.alpha 1;
  }
  
  if (
temp.dx && temp.dy 3) {
    if (
temp.dx && temp.dy <= 6) {
      
this.alpha 0.75;
    }
  }
  
  if (
temp.dx && temp.dy 6) {
    if (
temp.dx && temp.dy <= 9) {
      
this.alpha 0.5;
    }
  }
  
  if (
temp.dx && temp.dy 9) {
    if (
temp.dx && temp.dy <= 12) {
      
this.alpha 0.25;
    }
  }
  
  if (
temp.dx && temp.dy 12) {
    if (
temp.dx && temp.dy <= 15) {
      
this.alpha 0;
    }
  }
  
setTimer(0.05);



cbk1994 10-08-2011 04:03 AM

PHP Code:

function getDistance(temp.x1temp.y1temp.x2temp.y2) {
  return ((
temp.x1 temp.x2) ^ 2) + ((temp.y1 temp.y2) ^ 2)) ^ 0.5;


http://uploads.graalcenter.org/uploa...04.23%20PM.png

pig132 10-08-2011 04:21 AM

EDIT: Scratch that didn't need to use it.

Ran into another problem. It works great, but its pretty much doing the opposite. I've messed with the math in temp.done but its still not right. The closer I am to the player, the lower the alpha. Which is the opposite and I'm not sure how I can flip it? Also I think the distance needs to be converted to less than 1 (decimals under 1) but i'm stumped!

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
setTimer(0.05);
}

function 
onTimeout() {
  for (
temp.plallplayers) {
    if (
pl.level.name == null) continue;
    
//define distance formula
    
temp.= (pl.player.x) ^ 2;
    
temp.= (pl.player.y) ^ 2;
    
temp.= (temp.temp.b) ^ 0.5;
    
    
temp.done temp.c;
    
    
player.chat temp.done;
    
temp.pl.alpha temp.done;
  }
  
setTimer(0.05);


Sorry!

There's got to be an easier way of doing this:

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
setTimer(0.05);
  
player.alpha 1;
}

function 
onTimeout() {
  for (
temp.plallplayers) {
    if (
pl.level.name == null) continue;
    
//define distance formula
    
temp.= (pl.player.x) ^ 2;
    
temp.= (pl.player.y) ^ 2;
    
temp.= (temp.temp.b) ^ 0.5;
    
    
temp.done temp.c;
    
    
//player.chat = temp.done;
    
    
if (temp.5) {
      
pl.alpha 1;
    }
    
    if (
temp.&& temp.<= 10) {
      
pl.alpha 0.75;
    }
    
    if (
temp.10 && temp.<= 15) {
      
pl.alpha 0.5;
    }
    
    if (
temp.15 && temp.<= 20) {
      
pl.alpha 0.25;
    }
    
    if (
temp.20) {
      
pl.alpha 0;
    }
  }
  
setTimer(0.05);



cbk1994 10-08-2011 04:49 AM

something like...

PHP Code:

temp.range 20// 20 tiles away would be 0 alpha
pl.alpha max(0, (temp.range temp.done) / temp.range); 

You should probably also just use players instead of allplayers for this.

pig132 10-08-2011 05:07 AM

Awesome! Thank you that works so much better & smoother.

To use just 'players' would it be:

PHP Code:

for (temp.plplayers

?

cbk1994 10-08-2011 05:25 AM

Quote:

Originally Posted by pig132 (Post 1670285)
Awesome! Thank you that works so much better & smoother.

To use just 'players' would it be:

PHP Code:

for (temp.plplayers

?

Yes.

pig132 10-08-2011 06:58 AM

Okay cool, thank you very much for your help :D :D

fowlplay4 10-08-2011 07:21 AM

You can also use vectordist() to calculate distance. I.e:

temp.distance = vectordist({player.x, player.y, player.z}, {pl.x, pl.y, pl.z});


All times are GMT +2. The time now is 07:58 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.