![]() |
|
Int to (float) |
Ceagon Xylas
Member #5,495
February 2005
![]() |
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
![]() |
Try casting to float the divisor: bullet[0].velX / (float)2 instead of (float)bullet[0].velX / 2. -- |
Ceagon Xylas
Member #5,495
February 2005
![]() |
nope. still moves in whole int values... EDIT: |
Avenger
Member #4,550
April 2004
|
try float cast every single int value in there
|
ReyBrujo
Moderator
January 2001
![]() |
Usually casting the divisor is enough. Which compiler you are using? -- |
X-G
Member #856
December 2000
![]() |
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
Member #5,495
February 2005
![]() |
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) return fResult; |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
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
![]() |
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
Member #5,495
February 2005
![]() |
DanielH
Member #934
January 2001
![]() |
Rey you had casting the divisor '2' to (float)2 should he not use 2.0f instead? float a; a/2 is int division |
ReyBrujo
Moderator
January 2001
![]() |
Both should work. -- |
DanielH
Member #934
January 2001
![]() |
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
![]() |
It seems (a couple of tests done) that the assembler unit does the cast before the code is compiled. -- |
Ceagon Xylas
Member #5,495
February 2005
![]() |
Neither seems to work for my script, though. =/ |
ReyBrujo
Moderator
January 2001
![]() |
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
Member #2,030
March 2002
![]() |
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
Member #5,495
February 2005
![]() |
Ok, basically... here's what i got
|
|