Int to (float)
Ceagon Xylas

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

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

Ceagon Xylas

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

try float cast every single int value in there

ReyBrujo

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

X-G

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.

Ceagon Xylas

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

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

Stefan Hendriks

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

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

gnolam
Quote:

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

#include <stdlib.h>

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

Ceagon Xylas

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

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

Both should work.

DanielH

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

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

Ceagon Xylas

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

ReyBrujo

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?

gnolam
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;;
}

Ceagon Xylas

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}

Thread #490129. Printed from Allegro.cc