Forum Groups
  All forums
    Help & Feedback
      Work in progress
      Finished Art
      Non-Max related

Maxunderground news unavailable

Need some math help
show user profile  digs
I'm racking my brain over this - just can't get it to work in code

I have a distance, target.position - this.position (lets say it 96.5)

as that distance gets closer to 0, id like to rotate my camera from 0deg to -90deg

this is in c#, but the maths would be the same. does anyone have any suggestions? I may have been staring at the screen for too long 8 /

read 418 times
3/22/2015 5:48:26 AM (last edit: 3/22/2015 5:48:26 AM)
show user profile  gogodr
just set 2 limits
like 0 and 1000 for distance
and then you can use something like
float distance = Math.abs(target.position - this.position);
if (distance > 1000) distance = 1000;
float angle = -90*(distance/1000);

that way the angle will approach -90 as the distance reaches 1000 and will stay at -90

Hello there

beautiful ;3

read 404 times
3/22/2015 8:36:35 AM (last edit: 3/22/2015 8:36:35 AM)
show user profile  Garp
rot = dist * 90 / 96.5 - 90
(1) In real code, you'd replace 90/96.5 by its value (divisions are expensive).
(2) If rotation angles are in radians, replace 90 by pi/2.

read 393 times
3/22/2015 12:40:52 PM (last edit: 3/22/2015 12:41:42 PM)
show user profile  S. Silard
@ digsDo not forget to use floats, as if you do not, you'll would not get a proper value.
So if for example you use gogodr's solution, than 1000.0f- should be used at division, otherwise it will take it as an integer as default. And if you multiply or devide an Integer, the return value will be an integer. On the other hand, Garp's function is more optimized.

Congrats, you found my signature.

read 389 times
3/22/2015 1:06:51 PM (last edit: 3/22/2015 1:07:01 PM)
show user profile  gogodr
>S. Silard
True that, my bad. I forgot that you need to cast the number as a float. you can do that with the .0f
Floating point multiplication/division is not way more expensive, if more expensive at all than Floating point addition.

here are some interesting articles on that:

Hello there

beautiful ;3

read 385 times
3/22/2015 1:20:18 PM (last edit: 3/22/2015 1:20:36 PM)
show user profile  Garp
> Floating point multiplication/division is not way more expensive, if more expensive at all than Floating point addition.

I agree with multiplication. Division is something else. Run some benchmark, you'll see.

read 378 times
3/22/2015 1:32:31 PM (last edit: 3/22/2015 1:32:31 PM)
show user profile  Garp
In gogodr's code, distance is of type float, so the division is fine (division of float by int converts to float).

read 370 times
3/22/2015 2:26:19 PM (last edit: 3/22/2015 2:26:19 PM)
show user profile  S. Silard
@ Garp - Distance is, but the limit is not.
Edit: But yeah, you are actually correct. My bad. But actually the result won't be that accurate.

Congrats, you found my signature.

read 360 times
3/22/2015 4:17:50 PM (last edit: 3/22/2015 5:29:50 PM)
show user profile  digs
awesome thanks guys, gonna eat some breakfast and get back into it
read 351 times
3/22/2015 5:10:10 PM (last edit: 3/22/2015 5:10:10 PM)
show user profile  digs
yep, these both work great. now my issue is with lerp never reaching 0 distance because of Time.deltaTime : P

go figure

thanks again!

read 341 times
3/22/2015 5:37:19 PM (last edit: 3/22/2015 5:56:38 PM)
show user profile  S. Silard
If you need a Minimal distance, than using Garp's function, it would look like that:

float GetCameraAngle(float Distance)
float MinDistance=100;//Minimal Distance
float MaxDistance=5;//Maximum Distance
float rot = ((Distance-MinDistance) * (90.0f / (MaxDistance-MinDistance))) - 90;//The Function
return rot;

P.S.: I'm into C++, not into C#; So I'm writing a C code here. Assuming it's correct.

Congrats, you found my signature.

read 335 times
3/22/2015 6:21:13 PM (last edit: 3/22/2015 6:21:13 PM)
show user profile  digs
Got it! nearly all the references (including official unity docs and tutorials) implement Mathf.Lerp incorrectly. setting the time value to a percentage from 0 to 1 properly lerps between distances

thanks again, all day yesterday proved to be less productive than 1 hour today : )
read 330 times
3/22/2015 6:42:26 PM (last edit: 3/22/2015 6:42:26 PM)
show user profile  Garp
> But actually the result won't be that accurate.


read 297 times
3/23/2015 2:41:28 AM (last edit: 3/23/2015 2:41:28 AM)
show user profile  S. Silard
@ Garp- All I meant that in that example the divider is the maximum distance. Thus if it's a variable, it has to be float, so it can be set to any value.

Sorry, English isn't my first neither second language. My reply might have not been clear enough.

Congrats, you found my signature.

read 291 times
3/23/2015 4:37:19 AM (last edit: 3/23/2015 4:37:19 AM)
#Maxforums IRC
Open chat window