Float and Int conversion

Started by bx83, Tue 13/07/2021 10:24:03

Previous topic - Next topic

bx83

I'm trying to work out some vectors:

Code: ags
//defs
  int tmx, tmy, cx, cy;       //x,y coords of tankman (tmx,tmy) and yourself (cx,cy)
  float Ftmx, Ftmy, Fcx, Fcy;    //their float equivalents

  float vc[4];           //define direction vectors
  vc[0]=-1.0;
  vc[1]=-1.0;
  vc[2]=-1.0;
  vc[3]=-1.0;  //set to -1 so non-existant directions don't come back as shortest vector ie. 0 distance
  
...

  Ftmx=IntToFloat(tmx);   //get Float values from original ints
  Ftmy=IntToFloat(tmy);
  Fcx=IntToFloat(cx);
  Fcy=IntToFloat(cy);

  if (C.up==1) {     //if we have direction up...
        cy--;
        if(cy<0)cy=9;
        vc[0] = Maths.Sqrt((Fcx-Ftmx)^2 + (Fcy-Ftmy)^2);      <----- ERROR! Type mismatch: cannot convert 'int' to 'float'


Wait what?
I tried putting in ^2.0 instead of ^2, but unsurprisingly this didn't work. Nothing else is not a float :/

Crimson Wizard

^ operator is not a "power" in AGS script, it's a "bitwise XOR" and only works with integers:
https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#operators

bx83

How do you do 'to the power of 2' / 'squared'?


bx83


Khris

Note that it is much faster to simply do

Code: ags
  float dx = Fcx-Ftmx, dy = Fcy-Ftmy;
  vc[0] = Maths.Sqrt(dx * dx + dy * dy);


Also note that you may not even need the square root. For instance checking if the distance/length is less than 10 simply means that the square needs to be less than 100.

SMF spam blocked by CleanTalk