Last active
December 14, 2021 14:32
-
-
Save DragoniteSpam/ca5c6c2f053a5e93d10e951c402ab19f to your computer and use it in GitHub Desktop.
Calculate the vector perpendicular to a triangle (GML).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function triangle_normal(x1, y1, z1, x2, y2, z2, x3, y3, z3) { | |
gml_pragma("forceinline"); | |
var v1x = x2 - x1; | |
var v1y = y2 - y1; | |
var v1z = z2 - z1; | |
var v2x = x3 - x1; | |
var v2y = y3 - y1; | |
var v2z = z3 - z1; | |
var cx = v1y * v2z - v1z * v2y; | |
var cy = -v1x * v2z + v1z * v2x; | |
var cz = v1x * v2y - v1y * v2x; | |
// if this is zero, the triangle has zero area and won't be visible anyway | |
var cpl = point_distance_3d(0, 0, 0, cx, cy, cz); | |
if (cpl != 0) return [cx / cpl, cy / cpl, cz / cpl]; | |
return [0, 0, 1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @param x1 | |
/// @param y1 | |
/// @param z1 | |
/// @param x2 | |
/// @param y2 | |
/// @param z2 | |
/// @param x3 | |
/// @param y3 | |
/// @param z3 | |
/// @return a three-element array containing the x, y, and z component of the triangle's normal. | |
gml_pragma("forceinline"); | |
var v1x = argument3 - argument0; | |
var v1y = argument4 - argument1; | |
var v1z = argument5 - argument2; | |
var v2x = argument6 - argument0; | |
var v2y = argument7 - argument1; | |
var v2z = argument8 - argument2; | |
var cx = v1y * v2z - v1z * v2y; | |
var cy = -v1x * v2z + v1z * v2x; | |
var cz = v1x * v2y - v1y * v2x; | |
var cpl = point_distance_3d(0, 0, 0, cx, cy, cz); | |
if (cpl != 0) { | |
return [cx / cpl, cy / cpl, cz / cpl]; | |
} | |
return [0, 0, 1]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just saw your comment. Here's the reminder ;).
Edit: anyways, it was already revamped. Thanks Dragonite for the always really useful (well not always always) content.