## As: Mechs - Vector Arithmetic

• 3,518 Views
• 27 Replies
Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
As: Mechs - Vector Arithmetic 2006-01-12 07:39:25

/*Assumed Knowledge: Understanding of object and classes, a basic grasp of trigonometry and calculus. There is no kinematics or real mechanics in this tutorial, but this tute is necessary for what I plan to write soon.*/

/*Note, where needed, Vectors will be written in rows and not in column form, for obvious reasons. What a bugger. */

What is a Vector?

A Vector is a defined by a magnitude and a direction. The magnitude is the size or "length" of the Vector, the direction is an angle taken from a reference point to denote where the Vector is directed towards. Examples of Vectors are displacement, velocity, acceleration, jerk, force and momentum.

Why Use Vectors?

Vectors are essential in mathematics, but not necessarily in OOP, so why use them? Using Vectors can give you extreme power such that you can use functions enabling you to apply external forces to objects without having to interfere with the object directly. For example, you could apply a wind force to an object and the object will internally calculate the acceleration due to that force, or perhaps you want a realistic collision between two bodies, and have your system calculate the resultant velocities of each bodies via momentum.

Setting Up

class Vector {

/*------------------
Variable Declaration
------------------*/

/*Numbers*/
public var _x:Number = new Number(0);
public var _y:Number = new Number(0);

/*------------------
Function Declaration
------------------*/

/*Constructor*/
function Vector(param_x,param_y) {
_x = param_x;
_y = param_y;
}

This is a basic 2D vector set-up. The _x and _y are the two components of the vector. IE: How far the vector moves horizontally and vertically. For example, a purely horizontal vector will have no vertical component, but say, an angled force can be considered to have some of it's force directed horizontally, and some of it directed vertically.

_x += param_vec._x;
_y += param_vec._y;
}

This function will use basic vector arithmetic to add another vector to it. If you have two force vectors, acting on one body, the resulting force will be the corresponding components of each vector added together. (example: somebody pushing a car with the exact same force that the wind is pushing against the car. The resultant will be that the car will stay stationary when the components are added together.)

//Scale - Factors The Vector By A Scalar Quantity
function Scale(param_scalar:Number) {
return new Vector(_x*param_scalar,_y*param_scalar);
}

This function returns the vector with its components multiplied by a constant value. This is useful in calculating thing such as acceleration from a known force, where the mass is a scalar quantity, and a = F/M . Acceleration.AddTo(Force.Scale(1/Mass));

Using a bit of trig, we can calculate the magnitude and direction of the vector should we ever need it.

//GetMagnitude - Returns The Absolute Value Of The Vector
function GetMagnitude() { return Math.sqrt(Math.pow(_x,2)+Math.pow(_y,2)); }

//GetDirection - Return The Absolute Direction From The Horizontal
function GetDirection() { return Math.atan(_y/_x)+Math.PI*((_x<0) ? 1 : (_y<0)*2); }

GetMagnitude is simply pythagoras' theorem, GetDirection uses a trigonometric identity to find the angle of direction clockwise from the positive horizontal axis in radians. This could be useful for finding the _rotation property of a vehicle based on it's acceleration vector.

Enjoy pissing around with this. I always like to put a zero function in for frame iterative purposes, like:

//Zero - Zeroes The Vector
function Zero() {
_x = 0;
_y = 0;
}

For Flash-related reasons (to people who care) I have excluded the cross and dot product from this tutorial. I'll be following this tute up with applications of Vector Math. After I do one on kinematics I can start doing some real mechanics.

~Sekky~

Denvish
Denvish
• Member since: Apr. 25, 2003
• Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 07:53:21

You forgot the link again =(

AS: Main

Also, please remember to put a link to this thread (and your other tut) in AS: Main if you want them included on the lists... otherwise I might forget about them when updating.

- - Flash - Music - Images - -

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 07:54:18

At 1/12/06 07:53 AM, Denvish wrote: Also, please remember to put a link to this thread (and your other tut) in AS: Main if you want them included on the lists... otherwise I might forget about them when updating.

Ok, will do. Love you Denny xx

Rustygames
Rustygames
• Member since: May. 7, 2005
• Offline.
Forum Stats
Supporter
Level 19
Programmer
Response to As: Mechs - Vector Arithmetic 2006-01-12 08:17:43

cant wait to read this later!

Rantzien
Rantzien
• Member since: Jan. 27, 2005
• Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 08:29:57

I actually never thought of putting it in a class. Good job and thanks for the idea =)

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 12:53:22

you haven't covered vector multiplication (cross, finds the normal of the force), vector dot multiplication (find the angle between two vectors), vector planes, and most importently, how to use vectors to make 3d in flash which is the most interesting part.

waiting for a sequel :D

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 13:01:20

At 1/12/06 12:53 PM, Inglor wrote: you haven't covered vector multiplication (cross, finds the normal of the force), vector dot multiplication (find the angle between two vectors), vector planes, and most importently, how to use vectors to make 3d in flash which is the most interesting part.

For Flash-related reasons (to people who care) I have excluded the cross and dot product from this tutorial. I'll be following this tute up with applications of Vector Math. After I do one on kinematics I can start doing some real mechanics.

Cross product is redundant in 2 dimensions. Dot product will be useful, only when I start on matrices. Planes don't even factor into 2D dynamics...and THEN you mention 3D............ quite a silly display of knowledge there Ingley.

waiting for a sequel :D

will do

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 13:11:37

At 1/12/06 01:01 PM, Sekky wrote: THEN you mention 3D............ quite a silly display of knowledge there Ingley.

2d vectors are useless for the most part :P you can represent them as a linear equation and it would be just as easy :P

as for dot, it is pretty simple
a:t(1,2)
b:t(4,2)
a dot b is 1*4 + 2*2

on a more general meaning
(x1,y1,z1) dot (x2,y2,z2) is
x1*x2+y1*y2+z1*z2

pretty easy, this is useful because in order to find the angle between two vectors all you have to do is
cos (thatAngle)=abs(vct1 dot vct2)/(sqrt(vct1*vct1) * sqrt(vct2*vct2));

this gives us one of the most importent rules
vector a squared is equel to it's scalar value squared. (just check the angle from a vector to itself, cos 0 is 1)

will do

:)

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 15:01:03

At 1/12/06 01:11 PM, Inglor wrote: Stuff

They're not useless, as you'll see when I expend these tutes. Although I could probably pull off "simulated" 3D vector calculus in AS, I definately wouldn't trust it to be neat enough to teach to others, so let me go through 2D mechanics in it's entirety, then I'll try expanding it, k?

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 15:05:38

At 1/12/06 03:01 PM, Sekky wrote: They're not useless, as you'll see when I expend these tutes. Although I could probably pull off "simulated" 3D vector calculus in AS, I definately wouldn't trust it to be neat enough to teach to others,

have you ever spoken to delta? he would love to show you a 3d engine that isn't simulated nor lame, it is quite nifty in fact, vectors are the way to go for 3d since you need more than one equation for 3d without em'

so let me go through 2D mechanics in it's entirety, then I'll try expanding it, k?

well, if you can come up with an interesting use for it, I would love to see it.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 15:09:10

At 1/12/06 03:05 PM, Inglor wrote: vectors are the way to go for 3d since you need more than one equation for 3d without em'

Dude I know, I study math

well, if you can come up with an interesting use for it, I would love to see it.

Just watch me, newtonian mechanics in barely any lines.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 17:14:22

At 1/12/06 03:09 PM, Sekky wrote: Dude I know, I study math

Just curious, but what math exactly are you studying at school / uni

I want to know if I'm gonna be learning this shit at some point.

InglorAntiPostLimit
InglorAntiPostLimit
• Member since: Nov. 23, 2002
• Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 17:16:48

At 1/12/06 03:09 PM, Sekky wrote:
At 1/12/06 03:05 PM, Inglor wrote: vectors are the way to go for 3d since you need more than one equation for 3d without em'
Dude I know, I study math

which just makes the fact you didn't treat them worse :D

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 17:26:55

At 1/12/06 05:14 PM, T-H wrote: Just curious, but what math exactly are you studying at school / uni

I'm finishing up my second year of high school. So far we've studied 10 modules, 6 in pure math, 2 in mechanics, 1 in statistics and 1 in discrete/finite math. I have two pure modules left to go. Or I may just take a few extra in the other areas for shit and giggles.

Pure Math is the study of mathematical proof and the like, it's really calculus heavy, and it sets you up well for the other areas. Towards the latter modules you get into some really challenging stuff, second order differential equations, matrix properties and arithmetic. It's great.

Discrete Math is essentially problem solving. Teaches you in logic, linear programming, game theory, set theory, combinatorics. This is my weakest area suprisingly, so I can't tell you much about it.

Mechanics and Statistics are applied Math. Mechanics is the study of forces and motion, how bodies act, studies of vectors, circular motion, center of mass, momentum, work and energy. There's a lot of calculus & trig in Mechs obviously. Statistics teaches you probability, distributions of data, Stats can actually be really interesting if you have the right attitude towards it and sift past the crap easily.

If you have any more questions just ask.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 17:53:53

At 1/12/06 05:26 PM, Sekky wrote: I'm finishing up my second year of high school. So far we've studied 10 modules.

Heh, I'm in england to, you don't have to use yankey-speak

Pure Math is the study of mathematical proof and the like, it's really calculus heavy, and it sets you up well for the other areas. Towards the latter modules you get into some really challenging stuff, second order differential equations, matrix properties and arithmetic. It's great.

Yeah, I'm on my second Pure / Core math module, starting 3rd next week apparently, we do 4 of those I think, 2 statistics (less interesting), one mechanics (looks good) one "decision maths" - which looks like absolute bullshit. and 2 (maybe more) "Further" modules - not entirely sure whats involved in those.

Discrete Math is essentially problem solving. Teaches you in logic, linear programming, game theory, set theory, combinatorics. This is my weakest area suprisingly, so I can't tell you much about it.

done a little set theory as part of stats, done some basic linear programming as well, but forgotten what that is like. not heard of game theory or combinatorics

Mechanics and Statistics are applied Math. Mechanics is the study of forces and motion, how bodies act, studies of vectors, circular motion, center of mass, momentum, work and energy. There's a lot of calculus & trig in Mechs obviously. Statistics teaches you probability, distributions of data, Stats can actually be really interesting if you have the right attitude towards it and sift past the crap easily.

Yeah, I hope I'm going to find mechanics interesting, I hear its just like one of the physics modules. I like calculus and trig, basically I like anything that I can actually apply to stuff I do (thus not a whole lot of statistics).

Thanks, sounds like I'm on a fairly similar schedule :D

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 18:10:49

At 1/12/06 05:53 PM, T-H wrote: Heh, I'm in england to, you don't have to use yankey-speak

Awesome stuff

Yeah, I'm on my second Pure / Core math module, starting 3rd next week apparently, we do 4 of those I think, 2 statistics (less interesting), one mechanics (looks good) one "decision maths" - which looks like absolute bullshit. and 2 (maybe more) "Further" modules - not entirely sure whats involved in those.

You do indeed. Decision math is discrete math. The first module is a bit of a shit, you do array sorting algorithms and stuff that resembles path finding, but really isn't. And whaaaat? You only do one mechs and two further pure modules? Btw further just means further pure, we're doing four core pure modules and four further pure modules, except we've done them in a stupid order. You'll like further pure if you like calculus.

done a little set theory as part of stats, done some basic linear programming as well, but forgotten what that is like. not heard of game theory or combinatorics

Nah you probably won't get into those. Game theory is in the second decision/discrete module, and combinatorics is like way ahead, but you get a lot of that stuff in later modules. I haven't studied it, I just read ahead. :)

Yeah, I hope I'm going to find mechanics interesting, I hear its just like one of the physics modules. I like calculus and trig, basically I like anything that I can actually apply to stuff I do (thus not a whole lot of statistics).

Statistics is applied Math. Everything you do in stats has a much a real world application as you do in Mechs, possibly even more so. You'll see. :)

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 18:22:47

At 1/12/06 06:10 PM, Sekky wrote: Btw further just means further pure

Ah see, makes sense as those books are titled "FP1" "FP2" etc.
and I just realised we do 12 modules total, 6 this year 6 next, so might be doing 4 FP's all together, but pretty sure just the 1 mechs and decision

Did my C1 exam on wednesday and got S1 next monday heh, before (S2 C2 C3 and possibly C4) in june :D

And I think I picked the wrong word in suggesting Stats can't be applied, as many of the questions use real word scenarios (which is a lot more than can be said for the pure questions, with the exception of arithmetic/geometric series), what I meant was that I find them less interesting as I doubt that I myself would use them as much as stuff like calculus and other co ordinate geometry stuff, which I can see having some use in game development. as well as mechanics stuff, projectiles and whatnot.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 18:32:56

At 1/12/06 06:22 PM, T-H wrote: And I think I picked the wrong word in suggesting Stats can't be applied, as many of the questions use real word scenarios (which is a lot more than can be said for the pure questions, with the exception of arithmetic/geometric series), what I meant was that I find them less interesting as I doubt that I myself would use them as much as stuff like calculus and other co ordinate geometry stuff, which I can see having some use in game development. as well as mechanics stuff, projectiles and whatnot.

Lol, projectiles are for babies :P Circular motion is the real shit.

Think of it this way, this is how I tend to think of it.

Once you're a proficient coder, there's three paths you can go down.

Stats - Primary Focus: AI (Probability). You can use Probability to create powerful Matrix based learning systems including Fuzzy logic. You will also be able to use advanced stats material in quantum probability.

Mechs - Primary Focus: Physics. You'll have a good grasp of forces and motion and will be able to create realistic, or completely made up, laws of physics. You'll be able to do colliding bodies and circular motion without thinking.

Discrete - Primary Focus: Algorithms. Discrete Math also plays a massive part in AI, being able to create finite state machines, neural networks and genetic algorithms. As another plus, your code will be neater.

Of course, if you rule like I do, you use everything, but your pure math always takes precedence.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 18:42:47

Haha cool. No idea about this "circular motion", I might have a look around now. Anyway thanks.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 18:49:52

At 1/12/06 06:42 PM, T-H wrote: Haha cool. No idea about this "circular motion", I might have a look around now. Anyway thanks.

Basically, if an object is moving in a circle, or an orbit, then it's linear velocity is always tangential to the circle, however it's easier to think of it as having an angular velocity in radians. In order for it to stay in a circular path, it must have an acceleration directed towards the center of the circle.

Obviously there's a lot of extra stuff that goes with that. I have an sample paper here 'cause I'm sitting this exam on Monday. I might post a couple of sample questions on this for you later if you wish.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 18:56:05

At 1/12/06 06:49 PM, Sekky wrote: Obviously there's a lot of extra stuff that goes with that. I have an sample paper here 'cause I'm sitting this exam on Monday. I might post a couple of sample questions on this for you later if you wish.

Yeah that would be great cheers! just to see what I'm in for in a year or so.

I see what you mean now with the orbits, delta showed me some examples a while ago and ranted a lot about the vectors involved, I'll have to dig that conversation out and take another look.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-12 19:17:50

Here's a circular motion one:

A toy car travels along a loop the loop track,

The track ABCD can be moddled as a continuous smooth surface contain in a vertical plane. The highest point on the track is A, which is a distance h above the floor. The loop is a circle of radius r with BC as a diameter, and with C vertically above B. The track ends at the point D.

The car acts as a particle of mass m, which starts from rest at A. In the case where teh car stays only just in contact with the track at C, it has speed u at B and speed V at C.

a) By considering the forces on the car at C, show that v^2 = rg

(mv^2)/r = mg, rearrange from there

mv^2/r is the angular force, mg is the weight reaction

b) Find an expression for u^2 in terms of g and r

Conservation of energy
(1/2)mu^2 = 1/2mv^2 + 2mgr
u^2 = 5gr

c) Show that h = kr, where k is a constant.

Conservation of energy again
mgh = 1/2mu^2
ect, I can't be bothered to work through it, it's easy though

d) Suggest one improvement that could be made to the model..yadda ect

Stupid garbage questions

Anyway, enjoy :P

Rantzien
Rantzien
• Member since: Jan. 27, 2005
• Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-13 07:43:59

Just thought of this. Since FP8, there is a Point class availiable that does a similar thing to this. The better thing with this is the customizability.

dELtaluca
dELtaluca
• Member since: Apr. 16, 2004
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-13 11:10:34

At 1/12/06 03:01 PM, Sekky wrote:
At 1/12/06 01:11 PM, Inglor wrote: Stuff
They're not useless, as you'll see when I expend these tutes. Although I could probably pull off "simulated" 3D vector calculus in AS, I definately wouldn't trust it to be neat enough to teach to others

http://denvish.net/u..355_APInotCollab.php

WASD keys + Drag mouse + SHIFT key

navigating a virtual camera round terrain with other players moving in constant direction and looping across terrain

all transformations for rendering are vector math with my own algorithm using straight line laws to clip polygons to edges and front and back, back face culling and static lighting done with planar math (planar equations found using vector math), as is the terrain collision using inequalities to determine which section of terrain the object is over, velocity of camera using vector math with friction and air resistance

At 1/12/06 05:26 PM, Sekky wrote: stuff
At 1/12/06 05:14 PM, T-H wrote: stuff

well.... im doing an additional mathematics exam this summer (half way between GCSE and AS) and the exam for first core (maybe second too) of AS, and im gunna see if i can take the normal GCSE exams this summer instead of next so i can just get them out of the way

but i also have a vast knowledge of vector and complex number math with a bit of quaternion math and matrix math with some knowledge on planar math and with the additional mathematics i can use calculus with any expression (but i dont know, and it wasnt in the core 1&2 book for AS on using integration and differentiation with expressions like sqrt(x^2-10/x) etc, only expressions like x^5/5 - 5x^2 etc)

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-13 11:41:47

At 1/13/06 11:10 AM, -dELta- wrote: sqrt(x^2-10/x) etc

youd just have to simplify that out, so

(x^2-10/x)^1/2
(x^2 - 10x^-1)^1/2 multiply powers
x - 10x^-1/2
so f'(x) = 1 + 5x^-3/2

I think...

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-01-13 12:08:16

At 1/13/06 11:10 AM, -dELta- wrote: but i also have a vast knowledge of vector and complex number math with a bit of quaternion math and matrix math with some knowledge on planar math and with the additional mathematics i can use calculus with any expression (but i dont know, and it wasnt in the core 1&2 book for AS on using integration and differentiation with expressions like sqrt(x^2-10/x) etc, only expressions like x^5/5 - 5x^2 etc)

Hah, you rule, I like you. I've never known anybody who gave a flying fuck about quarternions before they'd learned calculus.

That first expression, sqrt(...), equals (x^2 - 10x^-1)^(1/2) right?

The derivative is (x + 5x^-2)(x^2+10x^-1)^(-1/2)
The integral is (2/3)(1/((x^3)/3+10lnx)(x^2-10x^-1)^(-3/2) I THINK. That's not the easiest thing to do in my head. Don't quote me on that one

Btw, there's no need to distinguish between planar, vector, matrix, quarternion math, however you like, it's all linear algebra. :)

liam
liam
• Member since: Dec. 11, 2004
• Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-03-31 12:56:00

At 1/13/06 07:43 AM, Rantzien wrote: Just thought of this. Since FP8, there is a Point class availiable that does a similar thing to this. The better thing with this is the customizability.

The Point class is the same as this >=P

Heh =)

Sup, bitches :)

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Mechs - Vector Arithmetic 2006-03-31 13:21:14

Looks like someone here loves maths. :D