Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Int to (float)

This thread is locked; no one can reply to it. rss feed Print
Int to (float)
Ceagon Xylas
Member #5,495
February 2005
avatar

Ok, basically my setup is a blood engine, and when a bullet hits a character, the program runs a quick for loop and throws some blood peices everywhere... the problem is it shoots out in a very formed look. like the blood peices are distributed the same every few times. I know WHY it does this, but i cannot figure out where to put the (float) peice of code in this script:

//the roll() function returns an int
//bullet[0].velX and bullet[0].velY are both float
//blood[0].velX and blood[0].velY are both float

if(bullet<i>.velX>0)
  blood[0].velX=(bullet[0].velX/2)+roll(7)-roll(4)+2/33.3;
else
  blood[0].velX=(bullet[0].velX/2)-roll(7)+roll(4)-2/33.3;
blood[0].velY=bullet[0].velY-roll(4)+roll(2)+2/33.3;

I, later on, got it to work for a much simpler part of my code. Its for once the character's head is blown off, the jugular vein shoots blood everywhere:

blood[0].velX+=(float)roll(6)/8;
blood[0].velX-=(float)roll(6)/8;
blood[0].velY=-(float)roll(30)/10;

ReyBrujo
Moderator
January 2001
avatar

Try casting to float the divisor: bullet[0].velX / (float)2 instead of (float)bullet[0].velX / 2.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Ceagon Xylas
Member #5,495
February 2005
avatar

nope. still moves in whole int values...

EDIT:
would it have to do something with the return of the roll() function...sence it returns ints?

Avenger
Member #4,550
April 2004

try float cast every single int value in there

ReyBrujo
Moderator
January 2001
avatar

Usually casting the divisor is enough. Which compiler you are using?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

X-G
Member #856
December 2000
avatar

The problem is probably that roll(7) - roll(4) can only take on a particular low number of discrete values, so your resulting velocity will be one of those. Try generating a floating-point random value instead.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Ceagon Xylas
Member #5,495
February 2005
avatar

How do i generate a floating-point random value??

And rey, regretably, im using MSVC++ =[

Stefan Hendriks
Member #1,957
March 2005

i have had numerous problems like these as well. What i did, i simply changed my coordinates into floats. Basicly, if you keep it an integer, you can never 'change' your position properly. Ie:

int x=0;

x+=(float)some_function;

this will result in lets say 0.2

basicly will be rounded to 0 again. So you keep repeating ur 'error' and thus rounding up.

I also had problems when a function did not return some floating point properly when you use ints as input. What i did to fix that was:

float resultofsomefunction(int argument)
{
float fArg = argument;
float fResult=0.0f;
// do something here now with fArg and fill it into fResult

return fResult;
}

Jonatan Hedborg
Member #4,886
July 2004
avatar

something like rand()/(RAND_MAX+1) possibly without the +1. That would, of course, generate a number between 0 and 1

gnolam
Member #2,030
March 2002
avatar

Quote:

How do i generate a floating-point random value??

#include <stdlib.h>

float frand(float max)
{
  return max*(float)rand()/RAND_MAX;
}

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Ceagon Xylas
Member #5,495
February 2005
avatar

Thanks guys.
Stefan, i have my coordinates already set to floats.
Gnolam, how would i write that to work as this function?:

int roll(short sides)
{
  static bool seeded;
  int val;
  if (!seeded) {
    srand(time(NULL));
    seeded=TRUE;
  }
  val = rand() % (100000);
  val = (val % sides);    
  return val;
}

DanielH
Member #934
January 2001
avatar

Rey you had casting the divisor '2' to (float)2

should he not use 2.0f instead?

float a;

a/2 is int division
a/2.0f is float division

ReyBrujo
Moderator
January 2001
avatar

Both should work.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

DanielH
Member #934
January 2001
avatar

Just out of curryossity: What would the compiler do? Would it encode 2.0f and (float)2 as the same? I don't know much about compilers.

ReyBrujo
Moderator
January 2001
avatar

It seems (a couple of tests done) that the assembler unit does the cast before the code is compiled.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Ceagon Xylas
Member #5,495
February 2005
avatar

Neither seems to work for my script, though. =/

ReyBrujo
Moderator
January 2001
avatar

Can you create a simple program similar to the code you have (not even it needs to use Allegro, just a for cycle printing the values), so that we can check what you are writing, what you want to do, and why it is not working?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

gnolam
Member #2,030
March 2002
avatar

Ceagon Xylas said:

Gnolam, how would i write that to work as this function?

Quick copy & paste job (but with comments):

float roll(int max) //There's no point in using a short here. Just use an int or a float.
{
  /*
     I suggest you just call srand(time(NULL)) at the beginning
     of your program and be done with it - there's no use doing
     this check every time you generate a new random number.
  */
  static bool seeded;
  if (!seeded) {
    srand(time(NULL));
    seeded=TRUE;
  }

  return max*(float)rand()/RAND_MAX;;
}

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Ceagon Xylas
Member #5,495
February 2005
avatar

Ok, basically... here's what i got

1int roll(int i) {
2 //some code in here..then
3 return i;
4};
5 
6class {
7public:
8 float velX,velY,x,y;
9}blood,bullet;
10 
11void when_bullet_hits() {
12 if(bullet.velX>0)
13 blood.velX=bullet.velX/2.0f+roll(9)-roll(4)-4;
14 else
15 blood.velX=bullet.velX/2.0f-roll(9)+roll(4)+4;
16}

Go to: