Closest point on a line

Quick piece of code to find the closest point on a line segment from a point in 3D space: –

/// <summary>
/// Get the closest point (and distance) to line segment from a point in 3D space
/// </summary>
/// <param name="a_sourcePoint">Point that we're finding the closest point to</param>
/// <param name="a_start">Start of line segment</param>
/// <param name="a_end">End of line segment</param>
/// <returns>Closest point on the line segment and distance to the point on line from the source</returns>
public Vector3 PointOnLineSegment(Vector3 a_sourcePoint, Vector3 a_start, Vector3 a_end, out float a_length)
Vector3 ba = a_end - a_start;
Vector3 va = a_sourcePoint - a_start;
Vector3 w2 = va - ((ba * Vector3.Dot(va, ba)) / ba.sqrMagnitude);
Vector3 pointOnLine = a_sourcePoint - w2;

a_length = (a_sourcePoint - pointOnLine).magnitude;

return pointOnLine;





Dot Product

A simple definition of a dot product that I think works really well; “The dot product tells you what amount of one vector goes in the direction of another”

Might help someone with their understanding of what a dot product actually is


Unity Reading JSON Files

So, this one always seems to mess me around so I’m writing it up so I’ve got an easy reference for next time.

First we define the class we want to read the JSON into. This has to match to the format of the JSON (obviously) as it’s going to deserialize it. So, quick example might look like this: –

public class ContentCollection
    public Content[] contents;

public class Content
    public int id;
    public string stringStorage;
    public string moreStringStorage;
    public string[] arrayOfStrings;
    public bool boolStorage;
    public AnotherClass[] anotherArray;

public class AnotherClass
    public int id;
    public string extraStringStorage;

The JSON to go with this would look something like this: –

    "content": [
        "id": "1",
        "stringStorage": "First item",
        "moreStringStorage": "",
        "arrayOfStrings": [ "aString", "anotherString" ],
        "boolStorage": true,
        "anotherArray": [
            "id": "89",
            "extraStringStorage": "something here"
            "id": "90",
            "extraStringStorage": ""
        "id": "2",
        "stringStorage": "Second item",
        "moreStringStorage": "",
        "arrayOfStrings": [ "aString2", "anotherString2" ],
        "boolStorage": false,
        "anotherArray": [
            "id": "91",
            "extraStringStorage": "something else here"
            "id": "92",
            "extraStringStorage": "some more here"

Now we read the JSON using a standard Unity JSON reader. ‘json’ in this example is just a json string of the above JSON content.

using UnityEngine;

ContentCollection content = JsonUtility.FromJson<ContentCollection>(json);

All content is read in from the json and stored in an array of ‘Content’ classes which can be easily referenced as needed.

Best to note that Unity JsonUtility does not work with arrays that are stored at the base level of the JSON. It just ignores them and causes great confusion. Multiple posts about it and there are a few wrapper classes available to solve the problem as follows: –

public class JsonHelper
    public static T[] GetJsonArray<T>(string json)
        string newJson = "{ \"array\": " + json + "}";
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
        return wrapper.array;

    public class Wrapper<T>
        public T[] array;


Hope it helps







PHP: Heredoc

Heredoc is an easy way to echo html and Javascript content without having to worry about “‘s or adding ?> <?php all over the place. A simple example is as follows:-


// Lots of PHP code goes here

$id=57;  // Store the passenger number

<span class="blah">Heading</span>
<select name="project" class="moreBlah" onchange="DoSomething($id);">
echo $string;

// Lots more PHP code goes here


$string will store everything until it gets to the ‘END;’. The ‘END;’ MUST be on a line on its own and must be the first thing on the line.
Also, worth noting that you can just add php variables in place without any additional formatting (see above, just stick a $id in the middle of a Heredoc and it’ll come out as the correct value)

JavaScript: Post content to a php page

So, you’re working in JavaScript and you need to pass content to a php page. On the JavaScript side, you do this: –

var formData=new FormData();
formData.append('myString', myString);

var url="./accessMyString.php";

xmlhttp=new XMLHttpRequest();"POST", url, true);

This will store the string (or anything else you need to pass) into ‘myString’ in the FormData and will trigger “./accessMyString.php” passing the data as a POST.

In “accessMyString.php”, you’ll read content from the POST using the usual: –

$myString = $_POST['myString'];




PHP: Connect to a MySQL database, run a query and read a field

Quick reference for how to read a single field from a MySQL database in PHP: –

// Connect to the database
$mysqli = new mysqli(', 'schemaName', 'password', 'databaseName') or die ('<b>Could not connect: </b>' . mysql_error());

// Query the database for the info we need
$results = $mysqli->query("SELECT * FROM myData WHERE id='$id'") or die($mysqli->error);

$content = $results->fetch_assoc();
$field = $content['fieldName'];