• ## Object-oriented JavaScript

Any project using JavaScript tend to quickly grow out of proportions and often results in very messy code. There are some good tools out there for making it easier to write and maintain JS code, one of which I like the best: CoffeScript. Using these kind of tools helps a lot, but today I’ll present one way of making JavaScript more readable.

By applying object-oriented programming to JavaScript, it’s readability, modularity and reusability are greatly improved. Object-oriented languages has been around since Simula appeared in 1967, and is used by some of the most common languages like C++, C# and Java.

As JavaScript does not have native support for classes, but is able to use anything as as variable there are a couple different methods of making classes in JS. I’ll present the one that I think is most intuitive. Let’s get to it.

## Namespace definition

Using namespaces in JavaScript allows us to further separate the classes and avoids variable duplication. To do this, we need to define the namespaces as objects. To define the namespace “net.netxcius”, the following code is required:

## Class example

The following code defines our variable as a function. This is what will contain everything that we would normally place inside a class. The outermost function is called a self invoking function, this means that is calls itself upon creation. This is not required, but it has one great advantage: it can contain area specific global variables (Sarfraz Ahmed has a good blog post on this: Javascript Self Invoking Functions).

• ## Fast 2D Collision Detection in XNA

As shown in the YouTube video in the post Fast collision detection in XNA, I made a quick way of doing near pixel perfect collision detection in XNA that does not get slowed down by rotating the object. This algorithm first checks the bounds of the object that scales based on the rotation of the content(as shown in Re-scaling a rectangle based on the rotation of it’s content), before checking for line intersection (see Determining if two lines intersect).

The whole idea is to generate lines around the object as this is much faster then checking pixel by pixel and can easily be rotated as they only consist of two points. This is done only once when first loading the image.

Once the outlining has been traced, we generate lines between them to give us the approximate outlining. In this case 5 segments has been used to better illustrate what is being done. The mesh we end up with should look something like the following.

• ## Determining if two lines intersect

Today we are going to take a look at intersecting lines. There are some information on this scattered around the internet, but a good example is hard to find. The uses for this is mainly in 2D, it can be applied to lighting, physics and all other applications using rays.

To solve this, we want to convert our lines to the following form: $Ax + By = C$

This allows us to create an equation that we can solve, with no need for ray tracing or other techniques. Converting our lines to this form is pretty trivial, as shown below. We do this for both lines, giving us the equations for both.

Now when we have the expression for both lines, first of all we want to find the determinant to check if the lines are exactly parallel (they never intersect). The lines are parallel if the determinant is equal to zero.

• ## JQuery Tooltip

Ever in need of more than just the standard abbr tooltip? A little while back I wrote just that. It can easily be made prettier, but it works as intended. What this does is listening for the mouseover and mouseout events and display a div with absolute positioning at the element’s location with the tooltip text.

To add tooltips, the syntax would be like this:

This is what is looks like in action:

The JavaScript code that does the work looks like this. As you can see, the listeners are registered and when the mouse hovers the element, it gets the data-title and data-content attributes from the <a> element.

The full source code can be found here: tooltip_js.js. As always: If you have any questions, please let me know!

• ## Drawing lines in XNA

As there is no built in way of drawing lines in XNA(which is really weird), I thought that I should share this little snippet.

This function draws a line between p1 and p2, using the texture defined(yes, it needs to be a texture). The Color.White in the function, just specifies the clear color. I post this now, as it can be used with the upcoming how-to: Line intersection.

• ## Re-scaling a rectangle based on the rotation of its content

As you might have noticed in the video in my earlier post, the bounding box of the sprite is scaling based on the rotation. This is useful as it ensures that even if the content is rotated, it is always within the bounds of the rectangle. To illustrate this, take a look at the following photos:

When we rotate the blue square, its bounds increases to the square in green. This is what we are trying to compensate for. The following formula gives us the delta x and y from the origin point.

Using this formula, we find that dx’ = 4.24 and dy’ = 4.24. Please note that this formula will work for any sizes, I’m just using a square for simplicity. By applying this to code, we get the following in C#: