Lork

The Math Thread

Recommended Posts

Since the various advice threads in this forum seem to be divided up by engine (Unity, Gamemaker, Unreal etc.), I thought it might be useful to have a more general thread for asking and answering questions about the discipline that we all seem to either love or hate.

I'll kick things off with a question of my own. If I have a vector representing the direction that I'm facing and another vector representing the velocity of an object somewhere in the world, how can I use that information to find out how fast that object is moving perpendicular to me? To put it another way, I want to single out the speed at which the object is moving in a direction of my choosing, ignoring velocity in all unrelated directions. Because this seemed to cause some confusion when I asked about it on IRC, I've come up with an example:

Let's say that on a 2D grid, I am looking straight down the y axis. There is an object somewhere that is moving at a velocity (v) of (x=1, y=1). How can I find out how fast the object is moving along the x axis (p)?

6O0uoXI.png

In the example we can easily figure out that p is 1 by taking the x component of the object's velocity, but that is only possible because we're aligned with the grid. How can I find out what p is if I'm facing an arbitrary direction?

Share this post


Link to post
Share on other sites

EDIT: Dammit I got ninja'd.

 

I think you want a vector projection, though this is completely overkill because all you want is the x component of the vector. With this you can find the component along any direction in any dimension.

 

Assuming your velocity is a vector quantity that can be expressed as a linear combination including what you want to project it on, you can find its projection along the vector you want by multiplying the scalar projection:

dotprod2.gif

img5.gif

by the unit vector (of your projecting vector):

img6.gif

Where a(vector) is the direction you want to project onto and b(vector) is the direction you want to project.

 

(images stolen from http://www.math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/dotprod/dotprod.html)

 

So here your velocity vector is (1,1) and the vector you want to project onto is (1,0), then your scalar projection is 

g1Yqg4D.gifVGEOkEk.gifVQfsXbe.gif

 

You end up with 1, as you might expect. This method should work with any projection p and any vector v. It's just that if you're using x or y as your projection and you have the vector quantity of your velocity, you can just take the x or y spot in the vector and that's the projection.

 

 

...I hope I did that right.

Share this post


Link to post
Share on other sites

Thanks guys. I don't think I would've found that in a million years of searching, simply due to the fact that I wouldn't know which terms to start looking for.

Share this post


Link to post
Share on other sites

I might be handy to brush up (or learn from scratch) about vectors before delving into this!

 

> If I have a vector representing the direction that I'm facing.

This is a zero vector. A vector must have both magnitude and direction (your magnitude is zero since you are not moving). The first line of the wiki page that CEJ linked states

>The vector projection of a vector a on (or onto) a nonzero vector b

 

What you are really looking for is the projection of your vector v onto a unit vector parallel to the direction you are facing (for instance, the x axis).

Share this post


Link to post
Share on other sites

> If I have a vector representing the direction that I'm facing.

This is a zero vector. A vector must have both magnitude and direction (your magnitude is zero since you are not moving). The first line of the wiki page that CEJ linked states

IMHO, direction is represented as unit vectors, not zero vectors. A zero vector represented as (x,y) has no direction.

Share this post


Link to post
Share on other sites

I might be handy to brush up (or learn from scratch) about vectors before delving into this!

 

> If I have a vector representing the direction that I'm facing.

This is a zero vector. A vector must have both magnitude and direction (your magnitude is zero since you are not moving). The first line of the wiki page that CEJ linked states

>The vector projection of a vector a on (or onto) a nonzero vector b

 

What you are really looking for is the projection of your vector v onto a unit vector parallel to the direction you are facing (for instance, the x axis).

Not sure what you're talking about here. A vector is an abstract construct that is not limited to only representing velocity. A "zero vector" would be useless for indicating direction because, well, it doesn't indicate a direction. Instead a unit vector pointing in the desired direction is used, hence "A vector representing the direction I'm facing".

Share this post


Link to post
Share on other sites

Erkki, on 07 Sept 2014 - 13:01, said:

IMHO, direction is represented as unit vectors, not zero vectors. A zero vector represented as (x,y) has no direction.

Yes, this usually is the convention.

Share this post


Link to post
Share on other sites

I took a limit yesterday!

 

Listen, I've done my share of derivatives and integrals and assorted bullshit math that everyone thinks they are never going to use at work, but I figured that all the limits that needed to be taken have already been taken; then I wanted the terminal velocity in my fake physics system and blammo.  Pretty exciting.

 

I know what you're thinking: did it involve L'Hopital's rule?  The answer is yes.

Share this post


Link to post
Share on other sites

And it was just brought to my attention that i did a bad math! Sorry.

Share this post


Link to post
Share on other sites

At my university they decided to teach limits before teaching derivatives. It turns out that doing that stuff without L'Hôpital's rule is A PAIN. Like really, really hard. It involves all kinds of clever factoring and rewriting stuff as other stuff using non-trivial relationships and it's like one of those obtuse adventure game puzzles.

Share this post


Link to post
Share on other sites

Is it not standard to teach limits before derivatives? We were taught limits first in high school and it seems like you need limits to motivate what a derivative is and how it works: (f(x+h) - f(x)) / h as h -> 0.

Share this post


Link to post
Share on other sites

I don't remember how we did it in school, but you could definitely just teach that a derivative is the tangent of a curve and some rules for taking them before going back to formalize the concept using limits.

Share this post


Link to post
Share on other sites

You could go back to L'Hospital's after doing derivatives. If you're doing it formally, you'd do limits before derivatives. How would you even explain a derivative of a composition of functions without limits?

Share this post


Link to post
Share on other sites

Oh yes, of course conceptually and formally it makes sense to teach limits before derivatives. It just feels cruel to make students go through hell just to tell them "well, if I had just told you this simple rule you'd have solved all your exercises from last month in 5 seconds". I sure have learned a fair amount of stuff during my Engineering degree that I have almost no idea how to prove or even what's the theoretical background to support, so in a practical sense I don't see why teaching the "mechanics" of derivatives before limits would be a problem. You could always go back and show why that stuff works.

Share this post


Link to post
Share on other sites

Math-based textbooks (even those designed for non-strictly-math disciplines, like physics) are written by career mathematicians, or scientists with a mathematician's level of mathematical vocabulary.

 

That is why the language of those textbooks is obtuse. (sorry.) In order to be mathematically rigorous, you need to use a very specific vocabulary of words that mean very specific things, and you need to have an understanding of Discrete Mathematics that, in my experience, is barely taught outside of a college class room.

 

This means it's incredibly hard to understand what a math text book is attempting to say unless you know more math than the book's audience has. Mathematicians (for better or likely worse) want every student to understand the theoretical & logical underpinnings of every mathematical concept. This means you're going to learn the definition of a derivative (lim h->0 of f(x-h)- f(x)/h) way before you're given the power rule, even though every polynomial derivative you get from then on is going to be calculated via the power rule instead. Because a derivative means like 6 different things that all boil down to "a change that occurs over such an infinitesimally small time period that it is instantaneous," you have to start somewhere, and the agreed place to start is the mathematical statement (the definition of the derivative) that says exactly that.

 

I have a lot of thoughts and opinions about mathematical instruction. I graduated with a Mathematics degree & spent much in the year after graduating (under)employed as a math tutor for roughly middle school aged kids.

Share this post


Link to post
Share on other sites

Woah. I came here thinking "there could be some really useful information in here! I'll take a look!" and now all I can think is "I need to go take some math classes online..."

I love the idea for this thread though, definitely a helpful idea.

Share this post


Link to post
Share on other sites

Geez. I was awful at math at school, stopped in year 10, recently starting to feel okay about my math ability because of the UE4 middleware stuff I've been doing, and then I come in here 3204840swsw.gif

Share this post


Link to post
Share on other sites

Don't worry Lacabra, i have a phd in mechanical engineering and you saw how badly i fucked up the very cornerstone of a lot of mech eng concepts:P Ah, for the days when i could just scribble it all down in an exam and forget about it.

Share this post


Link to post
Share on other sites

Math, specifically linear algebra (and by extension numerical analysis) is basically my favorite thing ever.

Share this post


Link to post
Share on other sites

Alright, who wants to help me with some vector math!

 

The gist of what I'm trying to do is have players able to orbit around planetoids. I have that working, more or less, by way of applying force both downward toward the center of the planet, and forward in the direction of the players velocity, adjusted to be at the same radius as the players current position.

 

Note: GravityCenterTransform is the center of the planet. Also this is written in Unity C#

 

So, this is the downward vector:

Vector3 downwardVector = (GravityCenterTransform.position - body.position).normalized;

And this is the forward-ish vector:

float distance = Vector3.Distance(GravityCenterTransform.position, body.position);
Vector3 nextOrbitPt = body.position + body.velocity;
nextOrbitPt = (nextOrbitPt - GravityCenterTransform.position).normalized * distance;
orbitDirection = (nextOrbitPt - body.position).normalized;

Vector3 orbitVector = Vector3.Cross(downwardVector, orbitDirection).normalized;

Which is then applied to the players body (Gravity is an arbitrary value for setting the orbit speed)

target.Body.AddForce((downwardVector + orbitVector) * Gravity);

Of course looking at this code now, I can't remember why any pieces of it work. But it really nearly does!

 

The problem is that while you do orbit the planets, that orbit seems like its locked around one axis. If you try to adjust your velocity to make a new orbit, it always seems to magnet back to push the player along the same circular path, and any objects floating nearby enter the same orbit. This makes me think it's somehow a gimbal lock issue, but I'm not sure how to solve it.

 

Anyway if that explanation is too vague or is missing information please let me know and I can provide more detail or some drawings or if you're feeling really enterprising and want to poke at the code, I can put together a little demo. This has been driving me up the wall, so if someone is able to help my solve it I will probably buy them a gift.

Share this post


Link to post
Share on other sites

I'd like to help with this, could you point me towards the whole code? I'll have time tonight, 20.00 CET onwards.

Share this post


Link to post
Share on other sites

Ya, it would help if you gave us a little diagram of what you are trying to replicate^^ Start at the start and take it away!

Share this post


Link to post
Share on other sites

Here's a diagram? All objects, regardless of their starting velocity, end up in the same orbit. In terms of the code, that really is it; objects enter a trigger and the closest planet to them applies that force. I can't provide the entire source but I'll put together a small demo with the relevant stuff on Sunday night since I'm doing global game jam all weekend and I was supposed to leave for work 5 minutes ago.

aF2JJaf.png

Share this post


Link to post
Share on other sites

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