Code Snippets – Reflections

Reflections

Was learning vector math while the power was out and I came up with this code snippet.

Formula is:

/will add later.

Where I is the incident ray, N is the normal of a mirror and R is the resulting ray.

Code (in Java with LibGDX or similar libraries with vector math support):

private Vector2 CalculateReflRay(Vector2 incident, Vector2 normal) {
	float idotn = incident.dot(normal);
	Vector2 b = new Vector2(normal);
	b.scl(2*idotn);
	Vector2 result = new Vector2(incident);
	result.sub(b);
	return result
}

Code (in Lua):

function CalculateReflRay(ix, iy, nx, ny)
	idotn = ix*nx + iy*ny
	rx = ix - 2*idotn*nx
	ry = iy - 2*idotn*ny
	return rx, ry
end

Meshing in a Minecraft Game

0 FPS

The last post I wrote on Minecraft-like engines got a lot of attention, and so I figured it might be interesting to write a follow up.  This time I’ll try to tackle a different problem:

Meshing

One of the main innovations in Infiniminer (and Minecraft) is that they use polygons instead of raycasting to render their volumes.  The main challenge in using polygons is figuring out how to convert the voxels into polygons efficiently.  This process is called meshing, and in this post I’m going to discuss three different algorithms for solving this problem.   But before doing this, we first need to say a bit more precisely what exactly meshing is (in the context of Minecraft maps) and what it means to do meshing well.  For the sake of exposition, we’ll consider meshing in a very simplified setting:

  • Instead of having multiple block types with different textures and lighting…

View original post 3,098 more words

How to build a Skybase in Minecraft

In this article, I will show you how to create Skybases in Minecraft.

Note: This only works for servers, as in single player, you can’t go back up to your sky base without making a tower of blocks.

What you need:

  1. 2-3 stacks of sand/gravel (any block will do, but sand/gravel is easier as you will see shortly.)
  2. Any building material but in slabs (you can use full blocks but slabs are resource friendly.)
  3. A torch.
  4. A bucket of water.
  5. All the basic stuff you need to start a base (food, seeds, saplings, dirt, etc..)

What you must do:

Put a single torch on the ground, and put sand/gravel on top of it. The reason why is so that when you are done creating the skybase above, you can remove the sand/gravel tower by removing the block on top of the torch. Put any block on the ground and jump on the sand/gravel. Then just pillar jump using sand/gravel until you run out of it or reach a certain height (150 – 200 is a good height to build your base.) Note that some servers have lower height limits to reduce lag caused by sky bases. After you reach the height you want, put any block that isn’t affected by gravity on top of the sand/gravel pillar, /sethome, and place the water beside it. That way, you can go down, remove the block on the torch, and watch your sand/gravel pillar go down and break in pieces. After you are done with that, do /home and remove the water source (if you haven’t removed it when you got down.)

Now you can create your base! First thing you have to do is build a platform. You MUST use slabs instead of ordinary blocks since it saves resources (for example, 33 cobblestone can make about a stack of slabs.) Make sure you place the slabs upside-down. After that, you can make all the buildings you need on your sky base. Make sure you don’t fall! (unless you built your skybase above an ocean.)

Lua Physics for Dummies Part 2 : Collision Detection

Alright, in this part, we will talk about basic collision detection codes, namely, Distance Based Collision and AABB (axis-aligned bounding box) collision detection. In here, I will only talk about collision detection not collision reaction . Collision reaction is what happens after the collision.

AABB Collision detection

AABB collision detection is just the collision of two rectangles, that are not rotated, that is, “axis-aligned”. The code is pretty simple :

function AABBcoll(rectA, rectB)
    local ax2, bx2, ay2, by2 = rectA.x + rectA.width, rectB.x + rectB.width, rectA.y + rectA.height, rectB.y + rectB.height
    return ax2 > rectB.x and bx2 > rectA.x and ay2 > rectB.y and by2 > rectB.y
end

The code checks if two rectangles intersect one another. It does so by checking if the first rectangle’s bottom right corner intersects the second rectangle, and if the second rectangle’s bottom right corner, is no less than the first rectangle’s top left corner. The code only works if the rectangles’ coordinates is aligned to the top left corner. The problem of this type of collision detection, is that it will not work for non-rectangular objects. If you try using circles, and use the code stated before (while adjusting the coordinate values to suit for the center-aligned circle), and try to hit one circle with the other diagonally, it will register a collision even though they’re not yet “touching”. But, the next type of collision detection will fix that.

Distance Based Collision Detection

Distance based collision detection works perfectly for circles. It simply checks if the distances of the two centers’ are less than the sum of their radii. The code is this :

function distBasedColl(circA, circB)
    local dx = circB.x - circA.x
    local dy = circB.y - circA.y
    return math.sqrt(dx^2 + dy^2) <= circA.radius + circB.radius
end

But when your talking about fast moving games, you really want to optimize the code. The math.sqrt function is kinda expensive. A way to optimize this, is to square both sides of the inequality, like this :

function distBasedColl(circA, circB)
    local dx = circB.x - circA.x
    local dy = circB.y - circA.y
    return dx^2 + dy^2 <= (circA.radius + circB.radius)^2
end

Alright, one last final piece of code. Multiple Collision Detection.

Optimizing Multiple Collision Detection.

So, 2 things colliding is good, but may not be enough for most games. But there are good ways to do it, and bad ways. Let’s look at the worst way to do multiple collision detection using double for-loops.

for i=1, #array do
    local partA = array[i]
    for j=1, #array do
        local partB = array[j]
        -- collision code goes here
    end
end

So what’s wrong here? Well, let’s look at the checks we need to do when we have, for example, 4 objects. Here’s how the loop will go :

obj1 to obj1, obj2, obj3, obj4
obj2 to obj1, obj2, obj3, obj4
obj3 to obj1, obj2, obj3, obj4
obj4 to obj1, obj2, obj3, obj4

Testing an object by itself is just complete nonsense. You can try to add an if statement, but even so, here’s what it will look like :

obj1 to obj2, obj3, obj4
obj2 to obj1, obj3, obj4
obj3 to obj1, obj2, obj4
obj4 to obj1, obj2, obj3

Can’t find what’s wrong? Well, first your testing obj1 to obj2. But when you go to the next line, your checking obj2 to obj1! Surely, if obj1 isn’t colliding with obj2, then obj2 also isn’t colliding with obj1. By optimizing the code to look like this :

for i=1, #array-1 do
    local partA = array[i]
    for j=i+1, #array do
        local partB = array[j]
        -- collision code goes here
    end
end

The collision checks for the objects will now look like this :

obj1 to obj2, obj3, obj4
obj2 to obj3, obj4
obj3 to obj4
obj4 to nothing!

So that’s better. Obj4 checks collision to nothing, because all the other objects are checking collision to obj4.

In the next part, we will talk a bit about collision reaction, and boundaries.