Skip to content

Instantly share code, notes, and snippets.

private static Vector3 ViewportToWorldPointUnity (Vector3 point, float fieldOfView, float aspect, Vector3 from, Vector3 to, Vector3 up)
{
// The zNear / zFar magic numbers are what Unity's viewport always uses regardless of camera settings
var matP = Matrix4x4.Perspective (fieldOfView, aspect, 0.6f /* zNear */, 1000f /* zFar */);
// For some reason we have to reverse these coordinates
from = -from;
to = -to;
@karlgluck
karlgluck / VectorMagnitude.md
Created October 13, 2017 05:11
Approximate magnitude of 2d and 3d vectors

Vec3 Magnitude

These C# implementations of approximate magnitude with no square roots are actually slower than calling a native implementation in Unity, but if this were implemented intelligently in C there's a good chance they would be faster. In any case, they're here because it was a pain to get the magic numbers.

Accurate to within 4.5%.


public static float MagnitudeFast (this Vector2 self)
    {
@karlgluck
karlgluck / GvimOmniSharpSetup.md
Last active September 21, 2017 20:13
Sadly, Gvim + OmniSharp-vim is just not worth the hassle on Windows. Locks up for 1s+ every cursor movement.

How to set up Gvim + OmniSharp-vim on Windows 10

Install Gvim

Right-click the C:\Program Files (x86)\Vim folder and change its Security permissions to allow full control & editing -> You shouldn't have to give admin permissions to do anything else in this guide

Install pathogen.vim

Clone OmniSharp-vim to C:\Program Files (x86)\Vim\vimfiles\bundle\omnisharp-vim

@karlgluck
karlgluck / ECS.cs
Last active May 17, 2018 00:59
My quick and incomplete version of Overwatch's Entity-Component System from the GDC17 presentation. Mostly written to think about how this would be implemented in a language with reflection.
public class ECSEntity
{
public ArrayList Components;
public static ECSEntity Acquire ()
{
throw new System.NotImplementedException();
ECSEntity retval = null;
return retval;
}
@karlgluck
karlgluck / IsPointInside.cs
Last active July 13, 2017 21:06
Winding method for finding whether a point is contained by a 2D polygon defined by a vertex loop (C#/Unity)
public static bool IsPointInside (Vector2 point, Vector2[] polygonBoundary)
{
int i = 0, j = 1;
int retval = 0;
while (j < polygonBoundary.Length)
{
var pointI = polygonBoundary[i];
var pointJ = polygonBoundary[j];
int side = (pointJ.x - pointI.x) * (point.y - pointI.y) - (point.x - pointI.x) * (pointJ.y - pointI.y);
int upward = ((side > 0) & (pointI.y <= point.y) & (pointJ.y > point.y)) ? +1 : 0;
@karlgluck
karlgluck / google-spreadsheet-as-database.gs
Last active March 7, 2017 22:54
Easy way to make a key-value store in a Google Spreadsheet. Just publish as a webapp and use jsonp.
function doGet(request)
{
var lock = LockService.getPublicLock();
lock.waitLock(15000);
var ss = SpreadsheetApp.openById(request.parameter.id);
var sheet = ss.getSheetByName('Sheet1');
var obj;
var dataRange = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var dataValues = dataRange.getValues();
if (request.parameter.k && request.parameter.v) {
@karlgluck
karlgluck / Inverted-Program-Flow-By-Abusing-IEnumerator.md
Last active December 4, 2016 23:56
A novel control flow pattern for easily writing a game's primary state machine

I'm working on some side projects and have been toying with ideas for more efficient ways to write games in Unity with C#. One of the major hangups for some of my larger side projects is that once it reaches sufficient complexity, it's hard to tell exactly what state the app is in. Even the simple wallet app I built has this issue. I've come up with the beginnings of a solution so I wanted to write it down before it gets lost.

The key problem is that you want your program to respond to inputs that require waiting for a response from something (a webserver, a user, etc). There are a number of options in Unity with C#.

These first 4 options are variations on a theme of control "elsewhere" in the program letting your code know that something has happened:

  • Virtual Callback E.g. if you derive from a class, and override a method which gets called in response to an event
  • Reflection Callback E.g. if you declare OnMouseMove on a class and that function gets called whenever the mouse moves
  • **Callb
@karlgluck
karlgluck / Binder.cs
Last active March 7, 2017 22:55
Super simple predeclaration framework for Unity that I'm toying with! Basically you can just declare any delegate field with [Bind] on it and this will look around for a static function or single member on another class that implements it, and hook that up on launch.
using System;
using System.Collections;
using System.Reflection;
using UnityEngine.Scripting;
// [Bind] is used to annotate a static field or property of type Func or Action. It causes
// the value of that member to be mapped to a function somewhere else in the program with the
// same name as that field. The target function can be a static function or an instance
// function whose class has a static property/field of its own type named "Singleton".
//
@karlgluck
karlgluck / CollectTwitchGameStats.py
Created April 18, 2015 15:59
Script that can be run with crontab to collect viewer & channel statistics about the top games on Twitch.TV into CSV files
import requests, json
import os
import re
import time
import datetime
data_dir = os.path.dirname(os.path.realpath(__file__)) + '/data'
api_url = 'https://api.twitch.tv/kraken/games/top?limit=100'

Keybase proof

I hereby claim:

  • I am karlgluck on github.
  • I am karlgluck (https://keybase.io/karlgluck) on keybase.
  • I have a public key whose fingerprint is 6797 F0BC 1431 E8CB 5E34 D924 B4F7 4EB0 FF8B 9035

To claim this, I am signing this object: