GDevelop icon indicating copy to clipboard operation
GDevelop copied to clipboard

[SpriteRuntimeObject] Optimize custom hitboxes vertices transformations.

Open D8H opened this issue 3 years ago • 14 comments

Each time a point is transformed, the current implementation do a lot of calculus to apply an affine transformation.

https://github.com/4ian/GDevelop/blob/bc606ed1be28275624678dd7c6bcc192c2da846d/GDJS/Runtime/spriteruntimeobject.ts#L847-L883

The affine transformation can be calculated only once and reused for each point transformation:

    private _transformToGlobal(x: float, y: float, result: number[]) {
      result.length = 2;
      result[0] = x;
      result[1] = y;
      // @ts-ignore It's a FloatPoint at this point.
      this.getAffineTransformation().transform(result, result);
    }
    transform(source: FloatPoint, destination: FloatPoint) {
      //       x
      //       y
      //       1
      // a b c
      // d e f
      // 0 0 1
      const x = this.a * source[0] + this.b * source[1] + this.c;
      const y = this.d * source[0] + this.e * source[1] + this.f;
      destination[0] = x;
      destination[1] = y;
    }

Using the AffineTransformation class also make the code easier to follow (once used to the composition order).

It needs more tests and more features on AffineTransformation, but I wanted your view on this. I think it could be interesting to have it also in the base class.

I couldn't find any library that allows instantiation-free affine transformations so I started to implement this one.

A project example to check: https://www.dropbox.com/s/ygqc3m4nv9nb221/CustomHitboxTransformation.zip?dl=1

D8H avatar Nov 19 '21 19:11 D8H