Code / Development / My Own Stuff / Unity September 25, 2013 @ 12:01

Clamped Bezier Spline for Unity

There are probably a many more of these BSpline implementstions to be found on the interwebs, so here’s another one:

using UnityEngine;

public class BSpline {
   
    private BSpline() { }
   
    public static Vector3 GetPoint(Vector3[] cvs, float t) {
        int segCount = cvs.Length - 1;
       
        float u = t * segCount;
        float v = u % 1;
        int seg = Mathf.FloorToInt(u);
       
        if (seg >= segCount) {
            v = 1;
            seg = segCount - 1;
        } else if (seg < 0) {
            v = 0;
            seg = 0;
        }
       
        Vector3 a, b, c ,d;
       
        if (seg == 0) {
            a = cvs[seg];
            b = cvs[seg];
            c = cvs[seg+1];
            d = cvs[seg+2];
        } else if (seg == segCount - 1) {
            a = cvs[seg-1];
            b = cvs[seg];
            c = cvs[seg+1];
            d = cvs[seg+1];
        } else {
            a = cvs[seg-1];
            b = cvs[seg];
            c = cvs[seg+1];
            d = cvs[seg+2];
        }
       
        return curveFunction(a, b, c, d, v);
    }
   
    private static Vector3 curveFunction(Vector3 a, Vector3 b,
            Vector3 c, Vector3 d, float t) {
        float it = 1 - t;
        float t2 = t * t;
        float t3 = t2 * t;
        float b0 = it * it * it / 6;
        float b1 = (3 * t3 - 6 * t2 + 4) / 6;
        float b2 = (-3 * t3 + 3 * t2 + 3 * t + 1) / 6;
        float b3 = t3 / 6;
        return b0 * a + b1 * b + b2 * c + b3 * d;
    }
}
Code / Development / My Own Stuff April 11, 2012 @ 13:13

Python in French

# Pour Rudy, qui ne comprends pas un mot de Python quand c'est en Anglais.
import sys
dites = sys.stdout.write
ouvrez = open
lire = 'r'
ecrire = 'w'
fichier = 'd:/temp.txt'
texte_a_ecrire = u'Bonjour beau monde!'

dites('Je vais ouvrir un fichier qui s\'appelle %s\n' % fichier)
objet_fichier = ouvrez(fichier, ecrire)

# Traduire quelques fonctions
ecrivez = objet_fichier.write
fermez = objet_fichier.close

dites('Et maintenant, je vais ecrire cette texte dans le ficher: "%s"\n' % texte_a_ecrire)
ecrivez(texte_a_ecrire)
fermez()

dites('J\'ai ecrit et ferme le fichier, maintenant je vais ouvrir le meme ficher et dire ce qui se trouve de dedans.\n')

objet_fichier = ouvrez(fichier, lire)

# Encore plus des traductions
lisez = objet_fichier.read
fermez = objet_fichier.close

la_texte_dans_le_fichier = lisez()
dites('La texte dans le fichier: %s' % la_texte_dans_le_fichier)
fermez()

dites('Et voila, je disais tout que je vourdrais dire, merci!\n')
Development / On the Web / Tips March 2, 2012 @ 09:20

WordPress Non-FTP Upgrades

Annoyed by having to enter FTP credentials every time you update WordPress or any of it’s plugins? Put this in your wp-config.php:

define('FS_METHOD', 'direct');

Might not work for you, but you could try, it did for me. Thanks to this post.

By the way, if you still have problems updating, make sure you set the correct access rights for your directories in wp-content.

Code / Development / My Own Stuff May 14, 2011 @ 22:52

Ardor3D: Squad Interpolation

Just ported a spherical cubic (quadrangle) interpolation method to be used with Ardor3D‘s quaternions. I shamelessly ripped it, so I might as well share it:

    public static Quaternion lerp(Quaternion q1, Quaternion q2, double t,
            Quaternion store) {
        Quaternion c = Quaternion.fetchTempInstance();
        Quaternion d = Quaternion.fetchTempInstance();

        q1.multiply(1 - t, c);
        q2.multiply(t, d);

        q1.add(q2, store);
        store.normalizeLocal();

        Quaternion.releaseTempInstance(c);
        Quaternion.releaseTempInstance(d);

        return store;
    }

    public static Quaternion slerpNoInvert(Quaternion q1, Quaternion q2,
            double t, Quaternion store) {

        Quaternion c = Quaternion.fetchTempInstance();
        Quaternion d = Quaternion.fetchTempInstance();

        double dot = q1.dot(q2);

        if (dot > -0.95f && dot < 0.95f) {
            double angle = MathUtils.acos(dot);
            q1.multiply(MathUtils.sin(angle * (1 - t)), c);
            q2.multiply(MathUtils.sin(angle * t), d);
            c.add(d, store);

            divide(store, MathUtils.sin(angle), store);
            store.normalizeLocal();
        } else {
            lerp(q1, q2, t, store);
        }

        Quaternion.releaseTempInstance(c);
        Quaternion.releaseTempInstance(d);

        return store;
    }

    public static Quaternion divide(Quaternion a, double n, Quaternion store) {
        if (0 == n)
            throw new ArithmeticException("Divide by zero!");
        return store.set(store.getX() / n, store.getY() / n, store.getZ() / n,
                store.getW() / n);

    }

    public static Quaternion squad(Quaternion q1, Quaternion q2, Quaternion a,
            Quaternion b, double t, Quaternion store) {
        Quaternion c = Quaternion.fetchTempInstance();
        Quaternion d = Quaternion.fetchTempInstance();

        slerpNoInvert(q1, q2, t, c);

        slerpNoInvert(a, b, t, d);

        slerpNoInvert(c, d, 2 * t * (1 - t), store);

        Quaternion.releaseTempInstance(c);
        Quaternion.releaseTempInstance(d);

        return store;
    }

Will Perone, thanks!

EDIT: The previous version had a divide-by-zero problem, which should be fixed in the code above.

Development / Tips March 13, 2011 @ 16:07

Script: Connect Maya Nurbs Curves


This simple python script does not actually attach two curves.
Instead, it matches the first and last two control vertices to ensure continuity in translation and velocity.
Which ends will be connected is determined by selection order and curve direction.

EDIT: Ignorant me, I just discovered that Maya already has a built-in command for this: alignCurve.
Read the rest of this entry…

Development / Tips February 7, 2011 @ 00:46

Setting up SVN on Ubuntu

On my quest to get a proper version control system installed on a cheap low-spec machine that’s always running, this is what I came up with so far. I’ll try to write a quick tutorial, just highlighting the steps you need to get it up and running. Shouldn’t be too difficult.

Before, I set it up using Apache2 on my Ubuntu machine, accessible from port 80 with a regular web browser, easy. But after a while it started being really slow and unresponsive.  After some poking around, I discovered 10 to 20 apache processes, each taking up about 5 mb. My little media-playing Atom pc was choking.

The solution I found was to get rid of Apache altogether for this purpose and just run svnserve. Much easier to set up a and quick as a bunny!

First, install Ubuntu. Leaving that up to you. You should probably know how to open a console: CTRL+ALT+F1 from the ‘desktop’ (ALT+F7 to go back to the desktop). Now we can tell the computer to do things.

Install Subversion (SVN):

sudo apt-get install subversion

Now create a repository to store our actual data. Replace “/path/to/svnRepository with the path you want to use:

svnadmin create /path/to/svnRepository

You might want to set up some protection by requiring a username and password. Type this in the console:

nano /path/to/svnRepository/conf/svnserve.conf

Nano is a simple text editor, use your arrow keys to navigate. Look for this line

# password-db = passwd:

and change it to this:

password-db = passwd:

Also, make sure you change this line

# anon-access = read

to this:

anon-access = none

Press CTRL+X to exit Nano. It will ask to save the file, press Y to confirm that.

Then we set up a password by editing the passwd file:

nano /path/to/svnRepository/conf/passwd

You’ll see some lines of text there, add one line at the end. I’ll assume your name is ‘John‘ and your password ‘ILikeLOL‘:

John = ILikeLOL

You can add more users if you like. Then again, exit with CTRL+X and press Y to confirm the write.

Protect the file:

sudo chmod 600 /path/to/svnRepository/conf/passwd

The only thing we need to do now, is start the actual svn server every time the computer boots up by creating a script as such:

sudo nano /etc/init.d/svnserve

Type in this command that will start the server as soon as the script is run:

svnserve -d -r /path/to/svnRepository

You know the drill, exit (CTRL+X) and save (Y). All done! Well, almost. Now we reboot the computer and you should have your own SVN server!

There is a fair chance that I missed something or this doesn’t work at all. If so, drop a comment below.

Development / Tips February 6, 2011 @ 22:27

Version Control

A version control system is a server-based system on which you can store multiple versions of files without having to rename them or sacrificing too much disk space. Initially designed to store text files (code), it can also store binary assets, like pictures or audio files just fine. So why not just backing stuff up with whatever came with your computer? Many people working with computer files have the problem of having filenames like this:

SomeDesign.psd
SomeDesign1.psd
SomeDesign2.psd
SomeDesign1_New.psd
SomeDesign1_New_Final .psd

To solve this you could add dates to the file (SomeDesign_2010_01_01.psd) names or just keep adding numbers.
Not too practical because you’d need to keep a description of every file somewhere. I used to name files like this and tried all sorts of naming methods. But you work fast, save an extra version that your client likes and eventually, it becomes a mess.

This is where a version control system comes in. You just keep the same name, add a short description and throw it on the server that stacks it neatly into it’s database, keeping all older versions. At any time you can get an older version by number, date or description without having to move around stuff. And it’s easy to back up as well.

For teams it has even more benefits. If you’re working together on the same set of files, a website or a game for example, you try to keep in sync by copying files over a network, figuring out which ones are updated. Version control lets you update a project, just altering the new files and warning you if somebody has been working on the same file as you did. You could even lock the file that you’re working on to make sure nobody else can write it meanwhile.

There are numerous version control systems available. To name a few from the top of my head: CVS, SVN, GIT, Perforce, etc. Some of them are commercially available and very good. Perforce for example is a fantastic system that handles all kinds of files really well and is extremely easy to set up, but it has a price tag. Git seems to be very good, but I haven’t looked at it (yet).

In short, if you’re creating a lot of file versions and your harddrives and your usb pen drives are becoming a relic of human chaotic organization… it’s probably worth looking into this.

If you want to use a version control system, you need some place to store your stuff. This can be done at a globally accessibleGoogle, Sourceforge, GitHub or whatever free or payed service you can find. Or better and more fun, you can set up your own at home or in your company’s network. I’m using Ubuntu on an low-spec Atom/ION-based mini computer, attached to my TV, that also plays my music and videos. I’ve written a quick guide to set up Subversion (SVN) on such a machine here.

Development / My Own Stuff January 23, 2011 @ 16:17

Find B-Spline Velocity/Tangent

As I’m not really good at maths, figuring this out took me a while; I wanted some code to let an object follow a long smooth path. But googling for hours didn’t give my any usable results.

One way to solve this, is to take two points on the curve and use the normalized difference to have a vector that is aligned to the curve. But this is imprecise and I wanted to do this properly.

Read the rest of this entry…

Development / My Own Stuff May 10, 2010 @ 21:28

Random Sentences

Click and then refresh for new nonsense!

Still work in progress, max number of words is 5.

Development / My Own Stuff May 5, 2010 @ 12:09

Box2DFlashAS3 Without “b2” Prefixes

Just refactored the Box2DFlashAS3 physics engine version 2.1a, removing the b2 prefixes to improve readability.
Of course in C++ (the original language in which Erin Catto wrote Box2D) it is probably practical to have unique class names, but since in AS3 we have a classpath, there really is no need to make class names more cryptical.

Get it here: Box2DFlashAS3 2.1a_refactored

Note that I just copied the package and only changed most of the class names, this means that the provided examples do not compile, you can still use them as a reference though.