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

Featured Threads
  inspiration alert!!!
(37 replies)
  Indespensible MaxScripts, Plugins and 3rd Party Tools
(37 replies)
  The allmighty FREE Resources Thread !
(17 replies)
  spam alert!!!
(4886 replies)
  Maxforums member photo gallery index
(114 replies)
  Maxforums Member Tutorials
(89 replies)
  three cheers to maxforums...
(240 replies)
  101 Things you didnt know in Max...
(198 replies)
  A Face tutorial from MDB101 :D
(95 replies) Members Gallery
(516 replies)
(637 replies)
  Dub's Maxscript Tutorial Index
(119 replies)

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 478 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 464 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 453 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.
read 449 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 445 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 438 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 430 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.
read 420 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 411 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 401 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.
read 395 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 390 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 357 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.
read 351 times
3/23/2015 4:37:19 AM (last edit: 3/23/2015 4:37:19 AM)
#Maxforums IRC
Open chat window