Jump to content

Changing Alpha Progressive


h4x7o0r

Recommended Posts

Hey there,

I don't know how can i change the alpha of an object gradually.

so considering i'm having an object and my car at different height one from another. Then i specify if the difference between my car height and that object height is higher than 4, that objects alpha to be changed progressive from 255 to 150.

At the moment, it's just a rough alpha changing like when it's > than 4 the alpha's 150.

How can i create that transition ?

Thanks a lot and hope isn't that ambiguous.

Link to comment
  
function setAlpha() 
local vehi = createVehicle(arguments) 
if getElementAlpha( vehi ) == 150 then   
setElementAlpha(vehi,255) 
else 
setElementAlpha(vehi,155) 
     end 
end 
addCommandHandler("toggle",setAlpha) 
  

If you type toggle it will create a vehicle. you need to fill it. Then it will check if its alpha if its 155 it will set vehicle's alpha to 255 else it will set vehicle's alpha 150.

Link to comment

How bout using onClientRender to set the alpha lower ?

like

  
local alpha = 255 
  
addEventHandler("onClientRender", root, 
    function() 
    if getDistanceBetweenPoints3D(.....) then 
    alpha = alpha - 10 
    else 
    alpha = alpha + 10 
    end 
    if alpha > 255 then 
    alpha = 255 
    elseif alpha < 150 then 
    alpha  = 150 
    end 
end) 
  

Link to comment
  • Moderators

Hi, here is the math calculation you need (It's fully configurabe):

local fullAlpha = 255 --alpha when diff = 0 
local diffLimit = 4 --over this limit, the alpha won't be reduced anymore 
local minAlpha = 150 --the alpha when the diff = diffLimit 
local _, _, z1 = getElementPosition( elem1 ) 
local _, _, z2 = getElementPosition( elem2 ) 
local diff = math.abs(z2 - z1) --height diff between the 2 elements 
local alpha = fullAlpha - diff * (fullAlpha-minAlpha) / diffLimit 
if alpha < minAlpha then alpha = minAlpha end 
--[[here will be the results: 
| diff  | alpha | 
|   0   |  255  | 
|   1   |228.75 | 
|   2   |202.50 | 
|   3   |176.25 | 
|   4   |  150  | 
... 
|   10  |  150  | 
]] 

Link to comment
  • Moderators
Thanks a lot for your replies guys.

Citizen's code works great but for example if diff = 0 there isn't fullalpha.

Really ?! how is that even possible ?! :shock:

Some math with diff = 0 :

alpha = fullAlpha - diff * (fullAlpha-minAlpha) / diffLimit 
alpha = 255 - 0 * (255-150) / 4 
alpha = 255 - 0 * 105 / 4 
alpha = 255 - 0 / 4 
alpha = 255 - 0 
alpha = 255 

So the alpha should be 255 !

Please use an output or a dxDrawText to print the alpha value. I'm really curious of what the value could be.

Link to comment

Hehe already fixed it. That "error'" was because of the distance between the car and the object, (has been calculated the dfference between car and center of the object which was a little bit high than i assumed).

Anyways thanks a lot for your help,

All the best.

Link to comment
  • Moderators
Hehe already fixed it. That "error'" was because of the distance between the car and the object, (has been calculated the dfference between car and center of the object which was a little bit high than i assumed).

Anyways thanks a lot for your help,

All the best.

You're welcome :wink:

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...