# Sabbatical Log: November 17th

**Posted:**2018/11/25

**Filed under:**code Comments Off on Sabbatical Log: November 17th

*This blog series is running about a week behind my actual work, giving me time to clean things up. The date the actual work was done is in the title, horizontal lines indicate when I stopped writing and started coding.*

This morning I got squirreled on more FixedPoint performance improvements. It occurred to me that the common Dot, Magnitude, and Normalize calculations could be optimized.

Precisely:

- Dot(P1, P2) = P1.X * P2.X + P1.Y * P2.Y
- Both X(1|2) & Y(1|2) are scaled, so they’re really (x(1|2) * SCALE) & (y(1|2) * SCALE)
- So our desired final product is [(x1 * x2) + (y1 * y2)] * SCALE
- Which we can get by calculating
- [(X1 * X2) + (Y1 * Y2)] / SCALE

- This saves a division per multiply and introduces one new division, for a net savings of one division

- Magnitude(V) = (V.X^2 + V.Y^2)]^(1/2)
- X and Y are scaled as above
- Desired product is [(x * x + y * y)^(1/2)] * SCALE
- We can get this by calculating
- [(X * X) + (Y * Y)] ^ (1/2)

- This saves a division per multiply and a multiply for the square root, for a net savings of two divisions and one multiply

- Normalize(V, (X|Y)) = V.(X|Y) / [(V.X^2 + V.Y^2)] ^ (1/2)
- X and Y are scaled as above
- Desired product is (x|y) / [(x * x + y * y)^(1/2)] * SCALE
- We get this with
- [(X|Y) * SCALE] / [(X * X + Y * Y) ^ (1/2)]

- Saving a division per multiply, a multiply per division, a multiply per square root, and introduces a new multiply, for a net savings of 2 divisions
- Additionally, I only calculate the denominator once and use it twice which saves another multiply

I also discovered a place where I was going over all *possible* entities, rather than just those in use, and fixed it – taking it from a 1,000 iteration loop to a few dozen iterations. So that’s nice.

I’ve decided on my next task will be to implement the sword (and it’s swings). This should get me to a good place for implementing an “enemy” (probably a bush, actually).

Again, lots of time spent extracting, formatting, and positioning assets – but I’ve got left and right sword swings triggering.

Clearly, some stuff still to be done. There’s no handling of a swing ending, or a player interrupting it, or anything like that – but it’s a start.

I’m choosing to model sword swings as spawning a new entity, because nothing has really done that to date – everything’s been spawned as part of either game start, or room creation. This will force me to go back and cleanup some entity management stuff later.