# Algebra math implementation in Game Engine

**URL:** <https://community.haxe.org/t/algebra-math-implementation-in-game-engine/2779>\
**Category:** Haxe\
**Tags:** haxe-hl\
**Created:** [November 20, 2020, 7:44pm UTC](https://community.haxe.org/t/algebra-math-implementation-in-game-engine/2779 "2020-11-20T19:44:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RexDex](https://avatars.discourse-cdn.com/v2/letter/r/45deac/32.png) [@RexDex](https://community.haxe.org/u/RexDex)\
**Post date:** [November 20, 2020, 7:44pm UTC](https://community.haxe.org/t/algebra-math-implementation-in-game-engine/2779/1 "2020-11-20T19:44:31Z")

</div>

Hello everyone!

We are evaluating Haxe as our scripting language with HashLink as target (compiled to C in final builds). Our current in-house scripting language has been working fine but it’s old and we don’t really have will power to maintain it in the long run.

Long story short, major concern is performance. Our current solution allows us to intechange data including structures between engine and scripting language. This allows us to do the following:

void VectorMulMatrix(Vector& vOut, const Vector& v, const Matrix& m) {  
vOut = v \* m;  
}

and expose it scripts as:

import struct Vector { // structure is “aliased” with C++ implementation “by hand”  
var x,y,z : float;  
}

import operator\* (a : ref Vector, b : ref Matrix) : vector;

Our old VM made sure that there are no copies of data made so when the VectorMulMatrix function is called the arguments point to memory managed directly by scripts.

How can I replicate this in Haxe/Hashlink?

Sepcifically:

- being able to declare “C” like structures that will directly alias engine data
- being able to pass this data in and out without any copying/marshalling/boxing etc.

If I can’t replicate it 1-1 what is the current best practice for this?

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![haxiomic](https://community.haxe.org/user_avatar/community.haxe.org/haxiomic/32/75_2.png) [@haxiomic](https://community.haxe.org/u/haxiomic)\
**Post date:** [November 21, 2020, 1:42am UTC](https://community.haxe.org/t/algebra-math-implementation-in-game-engine/2779/2 "2020-11-21T01:42:13Z")

</div>

Hey RexDrex, welcome!

I’ve not explored HashLink native interfacing yet but here’s an example for how you can achieve this with hxcpp (so the type is defined in C++ and passed around in haxe)

> **[HaxeFoundation/hxcpp](https://github.com/HaxeFoundation/hxcpp/tree/master/test/native/externs)**
>
> master/test/native/externs

For HashLink, I’d recommend checking out the source code for Kha

> <https://github.com/Kode/Kha/blob/master/Sources/kha/math/Matrix2.hx>

Which supports HashLink/C and hxcpp – I don’t know anything about the details however, @RobDangerous is the brains behind it all

You should be able to get pretty great performance out of native haxe vector math too – I’ve been writing this: [GitHub - haxiomic/vector-math: Write shaders in haxe: GLSL vector operations in haxe, complete with swizzles and all](https://github.com/haxiomic/vector-math) for my own projects – most vector and matrix elements wind up as stack variables when haxe is compiled so the they get auto-vectorized pretty well. That lib requires haxe 4.2 which is currently a nightly build [Haxe git builds](https://build.haxe.org/builds/haxe/)

---

<div class="post-metadata">

**Author:** ![Apprentice-Alchemist](https://community.haxe.org/user_avatar/community.haxe.org/apprentice-alchemist/32/1324_2.png) [@Apprentice-Alchemist](https://community.haxe.org/u/Apprentice-Alchemist)\
**Post date:** [November 21, 2020, 1:35pm UTC](https://community.haxe.org/t/algebra-math-implementation-in-game-engine/2779/3 "2020-11-21T13:35:59Z")

</div>

> being able to declare “C” like structures that will directly alias engine data

There are a few options for that, but all of them have inconvenients.  
You could add `hl_type *type` to each of your c structs, and then use classes in haxe.

```haxe
// c side
typedef struct {
   hl_type *type;
   int x;
   int y;
   int z;
} vector;

// in haxe
class Vec {
   var x:Int;
   var y:Int;
   var z:Int;
}

```

You could also use classes with the `@:struct` metadata without having to change your structs on the c side, but that messes up the signature of the function so you’d have to manually define the type string.

Or you could use abstracts + macros as I did in one of my projects :  
[C usage](https://github.com/Apprentice-Alchemist/KincHL/blob/060e132ea5ae0ef0b57d27ae46089d911eb69f4b/src/g4/texture.c#L4), [C macro](https://github.com/Apprentice-Alchemist/KincHL/blob/060e132ea5ae0ef0b57d27ae46089d911eb69f4b/src/kinchl.h#L26)  
[Haxe usage](https://github.com/Apprentice-Alchemist/KincHL/blob/060e132ea5ae0ef0b57d27ae46089d911eb69f4b/haxe/kinc/g4/Texture.hx#L7), [Haxe macro](https://github.com/Apprentice-Alchemist/KincHL/blob/060e132ea5ae0ef0b57d27ae46089d911eb69f4b/haxe/kinc/Macros.hx#L15)

> being able to pass this data in and out without any copying/marshalling/boxing etc.

Afaik that should work fine as long as you only pass around basic types and pointers.
