Hi,
To answer the easy bit first, using a float for a UVector2 value:
Code: Select all
// The UVector is built out of two UDim objects, each UDim object takes two
// float values; the first is a scale value, the second is an absolute pixel offset.
// either component can be set to zero if you just want one type of metric or
// the other.
// if you have a pixel float value, of say 100.0f...
float val = 100.0f;
// you can use this as input to a UVector2 like this...
UVector2 uvec2( UDim( 0.0f, val ), UDim( 0.0f, 0.0f ) );
// which sets the 'x' part of the vector to 100 and the 'y' part to zero.
I'm not quite sure how you have things arranged, what you're doing, or how you're doing it (so, off to a good start then!
).
The main uncertainty is as regards to what the float represents? Is it the deflection of the stick? So, perhaps it has a range of -1.0f to 1.0f? Or perhaps it has a different range, or even does not represent the deflection, hehe.
Basically, your cross is centred when the stick is centred - the first bit of info to consider is the position of the image when it is centred, meaning is it zero or some other value (such as half the width of the container). If this is not how you have things set up (which you may not, since it's probably not the 'best' way to do it), you'll have to adjust what follows.
Basically if your float is the deflection value, you can use it to scale some other value to calculate the position of the image. The example here is using absolute pixels and standard alignments, though there are numerous other ways to do this.
Say your container window for the cross is 200 pixels square. When the cross image is at position 0 it is centred. This means that to get a full left position, the image has to be moved left by 100 pixels (half the width of the container), and to get a full right deflection, the image has to be moved right by 100 pixels. If the float value represents the deflection of the stick in the -1 to +1 range, to calculate the final pixel position, just multiply the deflection value by the amount of pixel deflection needed - in this example 100. This means that if your stick deflection float is at -1.0 (for full left), you multiply this by 100 and get -100.0 back out which is the value to use for the image position. If the stick was half-way right, at +0.5f then multiplying this by 100 gives 50.0f which would put the cross at the desired position. And obviously the same for the vertical axis.
Now what do you do if your deflection value is not in the range -1.0 to +1.0? Well, the best way to proceed is to normalise it to that range. If your values are instead -500.0f to +500.0 then divide the value by 500.0f. If the range is something like 0.0f to 1000.0f then subtract half and then divide - so if you had a value of 0 (representing full left), you subtract half of the full range of 1000, which is 500 which gives -500, then divide by 500 which gives you -1.0f.
Hope this is helpful and that I put it in a way that's not more confusing
If it is confusing, please let us know what the float you have represents, it's range (if appropriate), and how you're positioning the cross (basically, where the cross is at position (0,0) and the size of the container holding the image).
CE.