Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Monday, 15 June 2009

Run a line through

I shall pick up where I left off in my previous post...

Right, in my previous post, it was about Billboarding, one of the techniques used in 3D Games. Professional games use this technique so well, it's very hard to tell whether they are using 3D models or just plane rectangles!

There are advantages. One very important advantage is that it reduces polygon count. If you burden the video card with too many things to draw on the screen, the game will lag. So, for very complicated objects, it's better to use billboard.

But when there are advantages, there should be disadvantages. Though you can draw a lot of details on a billboard, it's quite limited. What do I mean by this?

Imagine you want to draw a 3D object on a billboard. So you draw an image of a sphere as a texture for the billboard. As the billboard turns around to face your camera (your eye), it shouldn't be too much of a problem, since spheres are round however you see them.

But let's say you drew an image of a cube as a texture for the billboard. As the billboard turns around, the same image of the cube keep on facing towards you. It's as if the the cube is turning around to face you with the same... err... face! That'll look weird, unless you intended to do that.

Still billboards are very cool!

Hmm... let's get on with Axial Billboards (or Axis-aligned billboards). Axial Billboards can still turn around to face the camera, but they can only rotate around an arbitrary axis.

Assuming Z is up on the Cartesian coordinate system, if the billboard is aligned with the Z-axis, it's very simple to rotate it and face the camera. All you've got to do is get the horizontal direction from the billboard to the camera.

Using my previous diagram:
Point A is the position of the billboard object, B is the camera. All you've got to do is solve for angle y. Angle x is not needed.



Then draw a quad the same way as shown in the previous post, but this time, the transformations are as follows:
-Rotate around z-axis at "y" angle
-Translate to object's position

That is only for Billboards aligned with the Z-Axis.

For a billboard with an axis that is aligned with the x-y plane (horizontal axis), there are a few more steps in the calculations.


Oh my! WTF??

Chill, relax, sip some coffee. All will be revealed in a moment.

Line AC defines the axis for the billboard. So, r is the angle of point C from A.

Before I continue, I should tell you that because Line AC is parallel to the x-y plane,
Getting r could not be any more easier! Just a little trigonometry and...
Now, what about q? q is a little tricky. To solve it, we need to know the direction of B from A on the x-y plane. Then, subtract it from r.


So,
Since we have q, we can now solve for d.


Oh, we're almost there. We just need to solve for h before we can get angle p.

h is simply the difference of z value between A and B.
Now, using tan,

Finally, that horrible p!

Now, draw the quadrilateral:


Transform it as follows:
-Rotate it around x-axis at -p angle
-Rotate it around z-axis at r angle
-Translate it to its position.

Now, I may be wrong with the rotation around the x-axis. I get confused easily with the negative and positive angles, so if you find that I made a mistake, do tell me.

And that's that! But this only applies to axes that are aligned horizontally (parallel to the x-y plane, assuming z is up).

Gosh, such wonderful maths. Now, don't you think 3D games are cool? You better do!

Billboards are best used when the camera is not too close. Otherwise, its secrets will be revealed to the player. Well, it's not that bad, but it won't look too pretty.

Here, you can see the laser beam in Mr Ball 2:


It's an axial billboard!

Now over here, you can see how the beauty of billboards can be ruined:
The billboard passing over an object will reveal its "flatness", most of the time
Oh, and this one. Seeing the end of the billboard just kills the illusion, doesn't it?

Well, that's all! Lol!

Sunday, 14 June 2009

Random post

Well, I dunno what else to post, so I shall post some "numbers" again! Whee!

Seriously, if you hate maths, just don't look.

Remember a few posts ago, I talked about the so-called Axial Billboarding? It's a billboard aligned with an axis! Yay!

So far, I only manage to figure out how to orientate the billboard along an axis that is aligned to either the x-y plane or z-axis, assuming z is the "up" on the Cartesian Plane.

Before I continue on with Axial Billboarding, I should start with a normal billboard. A billboard is just a sprite that faces the camera wherever the camera goes.

Here, you can see the damage display (highlighted with blue circle). It's actually a billboard, because wherever I move the camera, it will face it.

So, how does the math go for this normal billboarding? Alright, we need to know 2 very important info, well, actually, 2 angles to be more precise.
Let's assume point A represents the position of the quadrilateral (usually a rectangle) that is going to be "billboard-ed".

Now, let's place the camera. Let's call the camera position, B.
Oh crap! So complicated! So many lines! Anyway, to put things in simpler terms,

Oh, I forgot to mention that line h and line d are perpendicular to each other. L is the distance from A to B.

Would you believe that from those 3 variables, we can solve for angles x and y, well, at least for x? Haha, here comes the magic of trigonometry (oh boy...)!

To solve for angle x, we can choose to use sin, cos or tan. It doesn't matter since we have all 3 sides of the triangle. But I would recommend using sin or cos because tan reaches infinity when the angle is equal to 90, 270, ...

Ok, let's use sin just because I had committed a lot of sins. Haha, get it? sine, sin! Lol! Ok, I shall stop.

So,

We got x! Now for y.

Angle y is basically the direction of B from A.


It's very simple to get y. See the "u"? u is the difference in x between A and B.

You can use the difference in y as well, but for that, you'll have to use sin instead of cos, which I'll be using.

Anyway,


We got x, and we got y!

With these 2 variables, we can finally turn the quad to face the camera. Now, it's very important to draw the quadrilateral with its centre as its origin to make it look good, unless, of course, you have your own reasons otherwise.

And, the quadrilateral should be facing the x-axis.

So, this will be how you'll draw your quadrilateral in a 3D game:



Next, you want to rotate it around the y-axis at x angle. Followed by a rotation around the z-axis at y angle. After all that, translate it to the object's position.

And that's all for the normal billboard. I won't go on with Axial Billboarding now because I'm just lazy to draw all the diagrams and blah. Maybe later.

Well, have a nice day!

Saturday, 18 April 2009

Circle, never ever underestimate it again

If you have learnt about circles, you will know that the Greek letter π is quite important. But π is an irrational number (a number that can never be expressed as a fraction). So, what we use is just the approximation in our daily lives, ie, 3.142 or 22/7. But as you can see, if you really want the very precise value of π those approximations will not be suitable for you. Last night, I was wondering, how to get the precise value of π?

Now, I discovered one weird looking formula last night, which may not give you the precise value of π when you demanded it, but at least you'll know how to get the precise value of π. And some more, this formula requires a very large number.

And so, here it comes, some wonderful maths. If you hate maths, just skip this whole post.

Before I begin, I shall introduce to you one cool formula.

A = ns² tan θ / 4

where
A = Area of any regular polygon (a polygon with equal sides and equal internal angles)

n = number of sides
s = length of a side
θ = 90(n-2)/n

That formula will give you the area of any regular polygon, given the number of sides and the length of a side. Remember, it only works for regular polygons.

Right, remember the area formula for a circle? That's right,

A = πr²

Let's get the area of a unit circle (A circle with a radius of 1):

A = π x 1 x 1
A = π

So, from there, the area of a circle with radius of 1 is equal to π
This is crucial to find out the weird formula for π later.

==============================================
Next, we go on to the very important part of this post:

Now, area of circles, are usually just approximations, because we use the approximation of π to calculate the area. So, they are not equal to the real area of the circle. Here's a better way to understand it:

Given a number 3.5, and you rounded that number up to 4, 4 is just and approximation of 3.5, but 4≠3.5

Imagine a situation where we require the precise value. Yea, so we need something.

Let's begin with a circle, with a radius of 1 (unit circle)


We'll start by approximating the area of circle, then we'll work our way to the precise value of the area of the circle.

Let's place a cyclic polygon into the circle. A cyclic polygon is basically a polygon which, the vertices touch the circumference of the circle.

Here's a regular cyclic triangle:


To find its area, we need the length of its side.

Here, I introduce to you another formula:

s=2r cos θ

where
s=length of a side
r=radius of circle
θ=90(n-2)/n

This calculates the length of a side of a REGULAR cyclic polygon, given the radius of the circle, and the number of sides.

In this case, the length of a side of the cyclic triangle is

s=2(1) cos 30
s=1.732

So, area of the cyclic triangle is:
A=ns² tan θ / 4
A=3(1.732)² tan 30 / 4
A=1.299

Ok, keep that in mind, for now.

Next, let's use another cyclic quad, maybe this time, we'll use and octagon.



Using the same technique, you'll find that the area of a regular cyclic octagon in a unit circle is:

A=2.828

Okay, it's easy to observe that as the number of sides (n) gets bigger, the value obtained for the area of the cyclic polygon will get closer and closer to the area of the circle. If we assume that π=3.142, the area of cyclic triangle has a bigger difference from π (Area of a unit circle) compared to the area of the cyclic octagon.

So, from there, we can conclude that the higher the number of sides of the cyclic polygon, the closer it gets to the area of the circle.

If we keep going on and on, with bigger number of sides, you'll know that there's a limit.

n, A:
3, 1.299038106
10, 2.938926261
100, 3.139525976
1000, 3.141571983
10000, 3.141592447
100000, 3.141592652
1000000, 3.141592654
10000000, 3.141592653

As you can see, the value of A gets closer and closer towards π, but never really reach it, as the number of sides increases. Therefore, it's safe to say that there's a limit here. And we can write it out like so:



And so, we have the formula for finding the value of π. The precision of the value you get from there depends on how large your value of n is. It's best to use n=1000000. But the bigger it is the better. But to work, θ&ne90. So, when you count for θ, don't round 89.9999999999 to 90, because that can yield weird results in the end.

Now, I don't recommend you using this in your daily lives though. It's just not practical. And some more, I don't really that I'm the first to discover this weird formula. Others may also have discovered it also, long before I did, so yea.

Thursday, 26 February 2009

Triangle, 3 angles of despair

Oh, sorry for the weird title. Anyway, the other day when I was walking back home with David Mosiun after the can rings hunt, he suggested to me about making the triangle a little bit more complicated. Yeah, so I wondered, why not?

Here's what I've done.

WARNING: If you an uncontrollable hatred towards Maths, I suggest you should look away.

First, let's place a triangle in the cartesian plane:

Simple enough? It's important that the labels for the vertices are in clockwise order. I'm gonna use vectors here, since vectors are so fun! No trigonometry will be involved (well, except for Pythagoras' Theorem)

First, run a line from point C down to line AB so that it is perpendicular to line AB.

That line (h) will divide line AB into 2 sections, a and b. What I'm gonna do now is find a, b and h. From there, I will be able to get the area of the triangle. Remember, I'm gonna do this without the help of trigo, since I'm on a trigo-hiatus. Hahaha.

We'll need the vector for line AB, so, it'll be

A-B.

Then, we'll need to normalize it.

W=(A-B)/(|A-B|)

Right, now, that'll be called W. To get the length of section a, we're gonna have to project line AC onto W. That's where dot product comes in handy. Because dot products can give negative numbers, which we don't really need, let's get the absolute value of it.

a=|(A-C) ּ W|

Same goes for section b, instead, it'll involve line BC.

b=|(B-C) ּ W|

Now for h. Since we know b. And c is just equal to |B-C|, we can get h:

h=sqrt(c²-b²)
h=sqrt[ (|B-C|)² - ((B-C) ּ W)² ]
Actually, since we are squaring those numbers, the absolute value sign is actually quite redundant, since squares are always positive. That's why I took out the absolute value sign. But note that I didn't touch the modulus sign for B-C in the (|B-C|) because that sign there represents the magnitude of vector (B-C).

Now we have a,b and h, let's put them together:

Area of triangle=1/2 x base x height

so,

Area of triangle
=1/2 (a+b) h
=1/2 (|(A-C) ּ W| + |(B-C) ּ W|) sqrt[ (|B-C|)² - ((B-C) ּ W)² ]

or, if you like a better view of this equation:

where,


Ok, so there's your "area of triangle" being turned into a garbled mess! Now, I don't guarantee that the equation above will be 100% accurate. There may be errors in there which I may have overlooked. You won't be seeing that equation in your textbooks anytime soon, so don't worry too much about it and blah. It's just for fun.

Wednesday, 7 January 2009

Let's talk math again

Right, today we (the whole class, and the whole form) went to the hall to listen to some talks about our personality, co-curriculum and discipline. Well, I got bored, so I started counting on my note book, which was supposed to take down notes from the talks. Lol!

Ok, first off, this post is just for fun, no bragging or boasting intended. If you really hate math, to the extend that you would bang your head against the wall whenever you see math, I suggest you look away.

And, I don't guarantee that my equations are 100% accurate, so, I don't recommend you to use my equations in your daily life. Use the ones you see in your Maths book.

Let's start. I'm not sure how this formula will benefit us. Maybe, it is beneficial in the game development world, but really, I don't know why I even bother with what I'm about to post up.

Anyway, I dunno how to say this, but it's something like How the circle is defined post. But instead of a circle, we are doing a square.

Imagine you have a square and a circle. That square fits perfectly into the circle forming a cyclic quad (the 4 vertices of the square touching the circle)

The width of the square, we will call it w.

w/2 is the distance between the centre of the square (which is also the centre of the circle) to one of the sides of the square, perpendicularly.

x will be the angle, which will be used to calculate all the other variables.

Note that the radius of the circle is the same as the distance between the centre of the square to one of its vertices, which can be easily determine by the Pythagaros' Theorem. When you do that, you should get w/sqrt(2)

Now, L is a little harder to explain.
Imagine you draw a line from the centre to one point on the circumference such that it is at x angle from the red horizontal line shown in the picture. Then, the distance from where that line intersects the edge of the square to the centre is L. And it is this distance that we are interested in.

Right, let's get started. Firstly, we want to find the value of a. To do that, we will take the radius of the circle and subtract it with w/2:

a=w/sqrt(2) - w/2

You should know how to add and subtract fractions with different denominators right?
Anyway, after subtracting the stuff, you should get

a=[w(2-sqrt(2)] / (2 sqrt(2))

Ok, another thing to remember:

L+b=w/2 + a

L+b=radius

Now, we need to experiment a little. Draw a few lines from the centre to the circumference.
Then, (referring to the picture on the right), see the green lines and the pinkish-purplish lines? First, just so you'd know, the red line is for reference. You measure the angle (x) from that red line to one line.

For the green lines, which start from the centre to the edge of the square, we will abbreviate the length of those lines L. And for the pale-purplish lines, their length will be called b.

Through observation, when x=0, 90, 180, 270...,
b is at maximum length. So,
b
= a
b = [w(2-sqrt(2)] / (2 sqrt(2))

or

b = [w(2-sqrt(2)] / (2 sqrt(2)) * 1

And when x=45, 135, 225, 315,...
b = 0

or

b = [w(2-sqrt(2)] / (2 sqrt(2)) * 0

Let's experiment more. We know that, cos 0=1 and cos 90=0. So, how are we going to incorporate it into the above equation?

Anyway, it's very hard to explain. But after testing and testing, it's not that hard to come up with this:

b = [w(2-sqrt(2)] / (2 sqrt(2)) * cos 2x

So, when
x=0, cos 2x=1
x=45, cos 2x=0

But then, when x=90, x=-1. We cannot accept negative numbers. So, we take the absolute value of cos 2x.

And, we will get:

b = [w(2-sqrt(2)] / (2 sqrt(2)) * |cos 2x|

So, now, we have the value of b. To get L, we subtract b from the radius, w/sqrt(2).

L = w/sqrt(2) - b
L = w/sqrt(2) - (
[w(2-sqrt(2)] / (2 sqrt(2)) * |cos 2x| )

Simplify it, and you will get:

L = ( w ( 2 - [2-sqrt(2)] * |cos 2x|) ) / ( 2 sqrt(2) )

I'm gonna keep testing this equation. And if it is flawless, then I should be able to derive the equations that define a square. In the Maths world, there should already be an equation which defines a square, but I prefer to discover it myself.

I have to say though, my equations may not be the best. There may be simpler equations out there. What I have just posted is just the product of my hobby, to discover new equations.

If you have read all the way until here, I admire your patience. Lol!

Saturday, 29 November 2008

Right, let's talk reflection!

Ever shined a torched light or a laser beam towards a mirror? If not, you probably have witnessed reflection by just looking into the mirror. Reflection of light, it's so fascinating and simple. But when it comes to math, it's a different story.

I'm been doing some paperwork on reflection of light, and I think I finally got it. Before I continue, there's gonna be a lot of Math mumbo-jumbo, so, again, if you hate Maths more than you hate the annoying little barking bitc.. dog next door, I suggest you look away. Lol! (No offense to those dog lovers, yeah, it's just a joke)

Here goes:

Let's say we have a beam of light. The beam's direction will be called as vector D. I'm going to be using a lot of vectors here.

There's also a plane of mirror, with a normal vector, N. This normal vector is a unit vector, ie, a 1-unit long vector. The normal vector is a vector that is perpendicular to the mirror.

Now, I shall draw it out. Introducing, MS Paint!!



I know, the normal vector is a little too long. I just drew it long so it's easier to see. But remember, vector N is a unit vector, as shown by the symbol above the letter N in the picture.

Ok, now, let's add more stuff.



On the left of vector N, there's another vector. Well, actually, it's just an inverted vector D, or -D. It's inverted for the calculations later. And there's one more extra vector, vector R, which is the reflection.

One very important thing that applies to all reflection:
The angle of incidence = angle of reflection

Therefore, a = b.

Because of that, the scalar projection of vector -D on vector N = the scalar projection of vector R on vector N.

I have coloured the so-called scalar projections in green.

Still with me so far?

The magnitude (length) of the scalar projections can be obtained through what's called the dot product.

The full stop (period) sign is a little too small for me, so to use as the dot product symbol, I'll utilise the "o" symbol instead.

Now, -D o N = |D| |N| cos a
But since N is a unit vector, thus |N|=1
-D o N = |D| cos a

which gives us the scalar projection of -D on N. According to
"the scalar projection of vector -D on vector N = the scalar projection of vector R on vector N."

we should now have:
-D o N = R o N

but we will use
R o N = -D o N instead. It's just the same, anyway.

Ok, that's 1 equation down, we will need 1 more.

The magnitude of the reflection vector (R) should be the same as the incidence vector (-D), so, here comes the equation (oh boy)

Rx2+Ry2=(-Dx)2+(-Dy)2

now, Rx means the x component of the vector. Ry means the y component. Same goes for the D vector.

Ok, I omitted the square root to make things simpler.
Now, we got 2 equations:

R o N = -D o N

Rx2+Ry2=(-Dx)2+(-Dy)2

Right, let's try with an example:



Right, I have normalized the normal vector, N. Normalizing is basically making a vector exactly 1 unit long. The vector is perpendicular to the mirror, therefore, it is "normal" to the mirror. So, "Normalize" and "Normal vector" are two different things.

Actually, N's direction is supposed to be (1/sqrt(2) , 1/sqrt(2)), but I was lazy to draw the root, so I got the decimal number instead.

So, let's get cracking.

R o N = -D o N

can be rewritten as

RxNx+RyNy = (-D)xNx+(-D)yNy

Substituting values, we get:
Rx(1/sqrt(2)) + Ry(1/sqrt(2)) = (-1)(1/sqrt(2)) + (2)(1/sqrt(2))
(1/sqrt(2)) (Rx+Ry) = (1/sqrt(2))

Dividing both sides by (1/sqrt(2)):
Rx+Ry=1

Dang, there are 2 unknowns!! What to do?? Chill man. That's where the 2nd equation comes in.

Rx2+Ry2=(-Dx)2+(-Dy)2

Plugging in the known values...
Rx2+Ry2=(-1)2 + 22
Rx2+Ry2=5

Ok, now, let's solve for Rx. First, get Ry.
Ry=1-Rx

Dump that into the previous equation:

Rx2+(1-Rx)2=5
Rx2+1-2Rx+Rx2=5

Remember the "completing the squares" thingie? Right, let's apply it here.
Now, make it so that it is in the form x2-2ax+a2, then it'll be easier.

2Rx2-2Rx+1=5
Rx2-Rx+1/2 = 5/2

To complete the square, take away -1/4 from both sides
Rx2-Rx+1/4 = 9/4

(Rx-1/2)2=9/4

Take the square root of both sides
Rx-1/2=3/2
So,
Rx=2

Placing this into Rx+Ry=1, and you'll get Ry=-1.

Therefore, the reflection vector is (2,-1). Makes sense right?

Woohoo!!! That was long! Now, I'm gonna do more research and probably refine these equations. This might not be the best way to calculate reflection, but at least it's something. Haha

Thursday, 6 November 2008

I'm rolling the world!

Sorry, I couldn't think of a relevant title for this post. This post is all about maths. So, if you hate maths, just look somewhere else. I'm not boasting or anything here, just bored only.

(David, you love Quadratics right? Well, here are some Quadratics for you!)

Right, do you remember about my post about ray tracing. Yeah, I tried implementing it, but it was too buggy, so I scrapped the project. The most simplest thing to ray-trace is a sphere. It's very simple to trace a line through a sphere. But, let's represent a sphere with a circle for simplicity okay.

Okay, a circle can be represented by this equation:
1) (x-x0)2+(y-y0)2-r2=0
where x0 and y0 represents the coordinate of the center of the circle in the Cartesian Plane.

I'm won't go through the details on why the circle's equation is like what I've typed above. If you know Pythagoras' theorem and the Cartesian grid, you should probably have a little understanding on how it's defined.

Next, the equation to represent a line. Here, I'll use vectors.
2) P=L+Dt
where
P is the (x,y) coordinate that lies on the line
L represents the line origin point (the line's starting point)
D is the direction of the line. This is a unit vector (a 1-unit long vector)
t is a scalar value. It's sort of like the distance from point P to L0

So, now, how do we check whether a line intersects with a given circle? First, we derive two formulas from equation 2.

3) x=Lx+Dx t
4) y=Ly+Dy t

If the line were to intersect the circle, there will be at least 1 coordinate that will satisfy both the circle equation and the line equation. Equations 3 and 4 are just to find the x and y components of the resulting vector P.

Anyway, let's plug those two babies into the circle equation:
5) (Lx+Dx t - x0)2 + (Ly+Dy t - y0)2 - r2=0

You're gonna hate what's coming up next. Now, you've got to expand it. Remember the (a-b)2 expansion rule? Hopefully you do because it's one very important rule. Well, Same goes for the (a+b)2 and all his cousins. Okay, you know what I mean right?

(a-b)2=a2-2ab+b2

By applying that into equation 5, you'll get a very big mess...
6) ( (Lx+Dx t)2 - 2(Lx+Dx t)(x0) + x02 ) + ( (Ly+Dy t)2 - 2(Ly+Dy t)(y0) + y02 ) - r2 = 0

This is one hard thing to type out in HTML man. Phew... got that down. Now, expand it some more to get an even bigger mess!!!

7) (Lx2 + 2LxDxt+Dx2t2 -2x0Lx - 2x0Dxt+x02) + (Ly2 + 2LyDyt+Dy2t2 - 2y0Ly - 2y0Dyt + y02) - r2 = 0

Starting to hate Maths already? Well, too bad. That's not the end of the horror.

Now, introducing the Quadratic equation:

8) Ax2 + Bx + C = 0
Now, instead of x, we need to find t.
So, equation (8) will be rewritten as

8) At2 + Bt + C = 0

To fill up the quadratic equation, we need the values of A, B and C. Where do we get such values? Do we like wish upon a star for it to come out? Do we dig underground to look for it? Not really. All you need to do is take equation (7) and factorise out t2 and t. After that, the remaining terms will be C.

So, factorising, you'll get:

(Dx2 + Dy2)t2 + (2LxDx + 2LyDy - 2x0Dx - 2y0Dy)t + (Lx2 + Ly2 - 2x0Lx - 2y0Ly + x02 + y02 - r2) = 0

You should now be able to see clearly which is A, B and C.
A = (Dx2 + Dy2) = 1
Mind you, the direction vector is a unit vector. This is just the Pythagoras' way of finding the length of the direction vector. In the end, you'll always get 1 for A (so long as the direction vector is a unit vector. Remember that)

B = (2LxDx + 2LyDy - 2x0Dx - 2y0Dy)
C = (Lx2 + Ly2 - 2x0Lx - 2y0Ly + x02 + y02 - r2)

Introducing, another pain in the ass (sorry David, I know how much you love Quadratics).

Now, that's the wonderful quadratics formula to solve for the value of "t". (Just substitute the "x" in the above equation with t)

Now, plug in the values that we have just obtained for A, B and C into a,b and c in the above equation and solve for t.

Once you have the value(s) of t, plug it into equation (3) and (4) to get the x and y coordinates of the intersection point(s).

One tip, by using the discriminant, b2 - 4ac, you can determine how many intersection points are there.

If...
b2 - 4ac = 0, there's only 1 intersection point
b2 - 4ac > 0, there are 2 intersection points, thus, 2 possible values for t
b2 - 4ac < 0, no intersections.

Here's an example:


Anyway, here's how to do it. Let's try with the red line. The starting point of the line (the origin of the line, L) is (-5,-5). The direction is actually supposed to be 1 over square root of 2 (1/sqrt(2)). 0.707 is just approximation.

The circle's center is at (2,2) and has a radius of 2. So, we got everything we need. Let's crank it up using the formulas above. First, let's get A, B and C!!
A=1 (direction vector is 1 unit long)
B= (2LxDx + 2LyDy - 2x0Dx - 2y0Dy)
So, B=-19.796

C = (Lx2 + Ly2 - 2x0Lx - 2y0Ly + x02 + y02 - r2)
So, C=94.

FYI, my calculations may be wrong. This is really one complicated formula, and because I'm always very careless, mistakes may occur. If you think I may be wrong, just ignore it. This won't appear in your Math syllabus.

Okay, let's find the discriminant:
b2 - 4ac = 15.88
And 15.88 > 0, so there are two intersections, as shown by the image above.

The value of t would be:
t=[19.796+sqrt(15.88)]/2 = 11.89
t=[19.796-sqrt(15.88)]/2 = 7.91

Now, slap the values of t into equation (3) and (4) to get the x,y values of the intersection points and you'll get

(3.4, 3.4)
(0.59, 0.59)

Those are the intersection points of the line and the circle. Remember, that's just an approximation. So, it's not 100% accurate, there's about 1% margin of error.

So, I hope you get it. It's interesting, no? You can try it out with the blue line.

Monday, 21 July 2008

Updated Mr Ball!!

Now, if you're thinking, why am I still updating it after 3 months from it's last update already? It's kinda old news already isn't it? Well, old news to you lah. But to me, Mr Ball's the product of my 1.5 months of planning and programming. I'm not just gonna leave it there and catch dust inside my computer. It's one of my biggest achievements in Game Maker. That's the first ever 3D game I have made, so I wouldn't just let it go rot away in my Hard Disk. Haha.

Anyway, I was bored just now, and played a bit of Mr Ball to entertain myself. But I found 1 very bad flaw in Level 6, the Spear Diamond thingie. If you keep shooting it, even if it has reached its Counter-Attack Threshold, it won't counter you until it stops receiving hits from your water gun. That is not good, since it'll make the level end incredibly fast. Because of that, I just tweaked it, so, regardless of how long you can manage to keep shooting him, as long as the Counter Attack Threshold bar is filled up, it'll counter you. If the boss's HP is like 1000+ HP, I wouldn't even care about this problem, but this boss only has like 500HP, the lowest amount of HP for a boss in this game.

I got so bored, I decided to record my battle with that boss, and here it is!!



Now, let's talk Maths!! I just got nothing better to do anyway. OK, this math-mojo is actually related to the video above. How? Okay, if you've watched the video, and if you played other games, a lot of them has objects going around another object in a circle (Circular motion around an object in err... shorter terms).

If you hate maths, i suggest that you look away now. This is just to let time pass by me. So, here goes:

Now, this is just a method on how to make things move in a circle in a game.



As you can see, that's a circle, right? Cool isn't it? I drew that in Microsoft Paint!! Haha. Yeah, I know, anyone can do better than that!! So, other than a circle, you should also be able to see the radius, marked "r". I hope you know trigonometry, because, I'm gonna go into trigs soon.

Right, the centre of the circle represents the position of Object A in a Cartesian Plane and the circle represents the series of points that Object B will move on. How do we make Object B move along those points on the circle? Haha, this is where Trigonometry comes in to play.

First, draw a line from the centre (O) to a point on a circle (P). When that line (OP) meets the circle, draw another line from the intersection point down to the radius, such that it intersects the radius perpendicularly, which will represent the y-coordinate of Point P relative to O. This is easier shown in graphics. Bring out Microsoft Paint again!!




There you go!! You can also see how the x-thingie is formed. So, OP is the same as r because OP is also the radius. Now, the angle subtended at the centre is abbreviated a. Using trigonometry, we know that:



Okay, now, a is the direction of Point P from the centre. Yup, the direction. How do we find the x and y values of the Point P given the direction from centre O. Remember, a direction of 0° is pointing to the right. What do I mean by that? Erm... shit, how do I explain this?? Anyway, just keep in mind, 0° is right. Then, as the angle goes higher, the rotating point goes counter-clockwise around the centre O. So, 90° will be at the top, 180° will be to the left and 270° will be pointing towards the bottom.

Yeah, so, we have the centre O with points (xo, yo). We are also given an angle a, which is the direction of P from O and the radius. The radius is just the distance of point P from O. To find Point P's coordinates relative to O, we use the above trigonometric formulas.

The coordinates of Point P, relative to O, given the direction and distance of that point from O can be calculated using:
xp=r cos a
yp=r sin a

Now, keep in mind that those are coordinates of P relative to O. To convert xp and yp into "world" coordinates, ie, coordinates that are relative to the origin of the Cartesian plane (0,0), you must add both coordinates with the coordinates of point O.

So, the x-value of Point P on the Cartesian Plane is: x=xo+r cos a
and the y value of Point P on the Cartesian Plane is: y=yo+r sin a

So, that's some math to ponder your brain!!