Search Unity

Overcoming The Curve: Complex Math

Discussion in 'General Discussion' started by Jofnir_IceSesh, Apr 30, 2017.

  1. Jofnir_IceSesh

    Jofnir_IceSesh

    Joined:
    Jan 27, 2017
    Posts:
    72
    So, I just recently spent the day messing around with normal mapping and all that fun stuff in blender.
    I am kind of balancing between artistic creativity and programmatic creativity. I want to be "The best of both worlds" in a sense ( I know this isn't something accomplished over night ). Id like to create good assets, and turn them into games as well.

    Out of the blue, I thought to myself, " Huh. I haven't made a missile yet, and missiles are cool ! ". So I began making it in Blender and texturing.

    Anyways, when all that's said and done, I decided to import it into Unity and perhaps attempt a challenge I haven't tried yet - Making my 3D model a homing rocket of destruction

    Here comes the curve that everyone loves to practice! Complex math equations. Especially with coding, the added complexity of having to nest certain things for a working product can make these complex math equations quite hard to wrap your head around. At least mine, that is.

    My wondering brought me to the oh so lovely wiki on Trajectory, with all sorts of scary numbers.

    So, how do I overcome it? How does one even begin to practice, to get up to the point of even being able to implement these equations into code format?

    What do you guys recommend? How can I practice and get visual results? How did you guys go from hardly being able to make a character controller, to being able to make trajectory and things alike?. Now, I know experience is the answer. But I guess I'm kinda just lost on where to start to even begin to gain the knowledge on how Id ever possibly do this kinda stuff.

    One would think trajectory, would be easier because of all its uses in video games (Grenades, bullet phys, sports, etc).

    Perhaps theres a concept or something I'm missing?
    General discussion about complex maths in unity and how to get past the curve (No parabolic pun intended.. lol)! :)
     
    Last edited: Apr 30, 2017
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    First it is important to remember that games aren't reality. It's more important that things like motion and animation 'feel' right, rather than be accurate in terms of the real world.

    That said, I like to start simple with math and expand as required to achieve the desired feel. Using the base physics and applying additional forces for example. Or precalcuating and tweaking. Things like that. Sometimes full math does work best, other times simple. I suggest starting with a goal of what you want, and doing it in the simplest (cheapest) way possible and then see where you are.
     
  3. Jofnir_IceSesh

    Jofnir_IceSesh

    Joined:
    Jan 27, 2017
    Posts:
    72
    Hmm Interesting. I definitly see what you mean by starting off small and working towards a finished product.

    Say however for someone like me who isnt adequate in using, say, trigonometry functions and what not. Mathf stuff. At the end of the day, I guess its just more visualization than raw numbers that helps me see whats going on more better
     
  4. HemiMG

    HemiMG

    Joined:
    Jan 17, 2014
    Posts:
    911
    There's a reason why Computer Science curriculums feature a crap ton of math. I've recently come to the conclusion that my math skills are far more rusty than they should be, after struggling for longer than I should have on an equation for my LumenLights asset. So I'm going to try to make my way through many of the free courses at Saylor's legacy section: https://legacy.saylor.org/
    Their main site has courses that aren't abandoned, and as such have interactive tests and potential for college credit, but there are much fewer choices.

    Of course, there is also the ever popular Khan Academy (which Saylor uses video from occasionally), but I have limited bandwidth and can't be watching videos all day long. Nor would I really want to, I can read faster.

    Anyhow, the point is that there are lots of places out there to learn or brush up on math, and it is absolutely a worthwhile time investment for a coder.
     
    Kiwasi likes this.
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    To visualize (and tweak), I do most of the math in the editor using handles/gizmos to visualize and see what is actually happening. Sometimes with things like trajectories and moving/reacting objects, it can be a challenge to "see" what happened. Math isn't my strongest area, I can do what I need and understand enough to google the right things. (I did art school). But there is a ton of good information out there, and trial and error is quite effective. ;)
     
    Martin_H and angrypenguin like this.
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    You're making it too complicated. You don't need wiki for trajectory.

    Instead, reduce your problem to newton's law of motion:

    1. F= ma (a = F/m)
    2. v1 = v0 + a*t
    3. x1 = x0 + v0*t + a*t*t/2.0

    That's it. v0/v1 - previous/next velocity, F - force, a - acceleration, m - mass, x1/x0 - next/previous position.
    All values are in a vector form.

    Because even weakest tablet today is more powerful than original cray supercomputer, you can calculate trajectory for anything in a very small amount of time by applying newton's equation in small steps (only in situation where F changes often. If F is the same, you can apply the whole formula in one step).

    So, basically, for a grenade thrower the idea would be:
    1. Aim in enemy's general direction.
    2. Calculate where the throw will land.
    3. Calculate distance between projected target position and the spot where grenade will end.
    4. Repeat until error is small enough or until you run out of attemps:
    4.1. Try adjusting starting parameters (heading/angle) by small value, and calculate point of impacts for them:
    4.2. Select the new value with lowest error.
    5. Launch projectile with calculated starting parameters.

    (I believe this is called newton's method: https://en.wikipedia.org/wiki/Newton's_method )

    And in case of homing missile:
    1. Repeat forever:
    1.1. Check where you're going.
    1.2. Accelerate towards the point where you want to go.

    And that's it.

    There's no complex math involved unless you decide that you want to collapse multi-step trajectory calculation into one step, and there is air drag involved (in this case you'll end up with differential equations).
     
    zombiegorilla likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Years of study and practice. Start with a high school physics book, and work your way up.

    It's worth noting that the real world doesn't really live by mathematics either. At best the math provides an interesting starting point. Then it's constant tweaking from there.
     
    zombiegorilla likes this.
  8. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Computer Science at my University was so close to being the same thing as Applied Mathematics I just added about a dozen classes my senior year so I would graduate with degrees in Mathematics and Computer Science not that there was ever much demand for 'just' a Bachelor's in Mathematics by any employer. When it comes down too it, Discrete Mathematics and Logic were more helpful for me to write good programs than numerical analysis and all the other mathematics. Now writing programs about mathematics was fun.