Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • 0
Simomarchi

[ACS] Problem with Fadeto function and fixed point values

Question

I'm making a script that is supposed to gradually fade the player's screen to black as he approaches a specific spot on the map.
The distance between the player and the spot is calculated with the following code (where 96 and 912 are the x and y coordinates of the spot, that is static of course):

 

Int x, y, distance, fade;

x = (GetActorX (0) >>16) - 96;

y = (GetActorY (0) >>16) - 912;

distance = start (x*x + y*y);

 

That code works well and returns exactly the value that I'm expecting as an integer (since the GetActor functions are converted to that format for simplicity).

 

The problem is that I use the following code to define the fade effect:

 

fade = 1 - (distance/1000);

Fadeto (0, 0, 0, fade, 0.0);

 

This code should start the fading effect when the player is 1000 map units from the spot but it just doesn't work because Fadeto requires a decimal number comprised between 0.0 and 1.0 to define the opacity of the screen and the variable fade returns an approximated integer value that is 0 or 1 (so no fade or pitch black).

ACS handles decimal numbers as fixed point values, so I tried to convert the fade variable in fixed point using:

 

fade = (1 - (distance/1000) <<16);

 

but again the number that is converted is an integer (so 0 or 1).

 

Lastly, if everything is calculated in fixed point there is still the problem that the int variable stores an integer, so FadeTo handles it in that way and set the opacity to 1.0 recieving 65000 and something as the value of fade.

 

Is there a way to store a decimal number in a variable? Or anything that can solve this problem?

 

Share this post


Link to post

3 answers to this question

Recommended Posts

  • 1

When you divide two int then the result will be an int. If the logical result would be a floating point number, the decimal places will just be discarded. So something like 0.25 or 0.31 will just be 0.

 

What you need to use here is the FixedDiv math function. Also check out FixedMul.

Share this post


Link to post
  • 0

That was the problem. Just in case that someone else will have the same problem in the future I've calculated the x and y coordinates of the player as integers, then I converted the distance in fixed point and lastly the fade variable was calculated with this code

 

fade = 1.0 - FixedDiv (distance, (1000 << 16));

 

The entire code was then placed inside a while (TRUE) to "refresh" the opacity of the screen every tick.

 

Thanks boris for your help.

 

Share this post


Link to post
  • 0
23 minutes ago, Simomarchi said:

1000 << 16

 

You can just write 1000.0 instead, which will make it more readable.

 

fade = 1.0 - FixedDiv (distance, 1000.0);

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×