Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions demos/demos/transform/2d/lookat.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ function updateLookers(world) {
target[0].x,
target[0].y
)

const offset = Rotary.fromAngle(-HALF_PI)

lookers.each(([orientation, transform]) => {
const position = new Vector2(
transform.x,
Expand All @@ -105,9 +106,8 @@ function updateLookers(world) {

// eslint-disable-next-line no-unused-vars
const [_, finalOrientation] = lookAt.decompose()
const angle = Rotary.toAngle(finalOrientation)

orientation.value = angle - HALF_PI
orientation.copy(finalOrientation.multiply(offset))
})
}

Expand Down
9 changes: 5 additions & 4 deletions demos/demos/transform/2d/propagate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
without,
has,
PI,
QUARTER_PI
QUARTER_PI,
Rotary
} from 'wima'
import { addDefaultCamera2D } from '../../utils.js'

Expand Down Expand Up @@ -85,7 +86,7 @@ function update(world) {

if(!parent || !child || !grandChild) return

parent[0].value += QUARTER_PI * delta
child[0].value += QUARTER_PI * delta
grandChild[0].value += PI * delta
parent[0].multiply(Rotary.fromAngle(QUARTER_PI * delta))
child[0].multiply(Rotary.fromAngle(QUARTER_PI * delta))
grandChild[0].multiply(Rotary.fromAngle(PI * delta))
}
3 changes: 2 additions & 1 deletion src/animation/core/animationeffector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @import { Entity } from '../../ecs/index.js' */
import { World } from '../../ecs/registry.js'
import { Rotary } from '../../math/index.js'
import { Position2D, Orientation2D, Scale2D, Position3D, Orientation3D, Scale3D } from '../../transform/index.js'

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ export class Orientation2DAnimationEffector extends AnimationEffector {
static apply(world, entity, results) {
const component = world.get(entity, this.componentType)

component.value = results[0]
component.copy(Rotary.fromAngle(results[0]))
}
static elementSize() {
return 1
Expand Down
6 changes: 4 additions & 2 deletions src/integrator/systems/euler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Query, World } from '../../ecs/index.js'
import { Quaternion, Vector2, Vector3 } from '../../math/index.js'
import { Quaternion, Rotary, Vector2, Vector3 } from '../../math/index.js'
import { Acceleration2D, Acceleration3D, Rotation2D, Rotation3D, Torque2D, Torque3D, Velocity2D, Velocity3D } from '../../movable/index.js'
import { Orientation2D, Orientation3D, Position2D, Position3D } from '../../transform/index.js'

Expand Down Expand Up @@ -57,7 +57,9 @@ export function updateOrientationEuler2D(world) {
const dt = 1 / 60

query.each(([orientation, rotation]) => {
orientation.value += rotation.value * dt
const angle = Rotary.fromAngle(rotation.value * dt)

orientation.multiply(angle)
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/integrator/systems/verlet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Query, World } from '../../ecs/index.js'
import { Vector2 } from '../../math/index.js'
import { Rotary, Vector2 } from '../../math/index.js'
import { Acceleration2D, Rotation2D, Torque2D, Velocity2D } from '../../movable/index.js'
import { Orientation2D, Position2D } from '../../transform/index.js'

Expand Down Expand Up @@ -33,7 +33,7 @@ export function updateOrientationVerlet2D(world) {
query.each(([orientation, rotation, torque]) => {
torque.value *= dt * 0.5
rotation.value += torque.value
orientation.value += rotation.value * dt + torque.value * dt
orientation.multiply(Rotary.fromAngle(rotation.value * dt + torque.value * dt))
rotation.value += torque.value * dt * 0.5
torque.value = 0
})
Expand Down
19 changes: 18 additions & 1 deletion src/math/core/angles/rotary.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Rotary {
* @returns {this}
*/
multiply(rotary) {
Rotary.multiply(rotary, this)
Rotary.multiply(rotary, this, this)

return this
}
Expand Down Expand Up @@ -313,6 +313,23 @@ export class Rotary {
return TAU - angle
}

/**
* @param {Rotary} a
* @param {Rotary} b
* @param {number} t
* @param {Rotary} out
*/
static slerp(a, b, t, out = new Rotary()){
const x = (a.cos + b.cos) * t
const y = (a.sin + b.sin) * t
const length = Math.sqrt(x * x + y * y)

out.cos = x / length
out.sin = y / length

return out
}

/**
* @param {Rotary} rot1
* @param {Rotary} rot2
Expand Down
4 changes: 2 additions & 2 deletions src/movable/prefabs/movable2d.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GlobalTransform2D, Orientation2D, Position2D, Scale2D } from '../../transform/index.js'
import { createTransform2D, GlobalTransform2D, Orientation2D, Position2D, Scale2D } from '../../transform/index.js'
import { Velocity2D, Rotation2D, Acceleration2D, Torque2D } from '../components/index.js'

/**
Expand All @@ -8,7 +8,7 @@ import { Velocity2D, Rotation2D, Acceleration2D, Torque2D } from '../components/
* @returns {[Position2D, Orientation2D, Scale2D, GlobalTransform2D, Velocity2D, Rotation2D, Acceleration2D, Torque2D]}
*/
export function createMovable2D(x = 0, y = 0, a = 0) {
return [new Position2D(x, y), new Orientation2D(a), new Scale2D(), new GlobalTransform2D(), new Velocity2D(), new Rotation2D(), new Acceleration2D(), new Torque2D()]
return [...createTransform2D(x, y, a), new Velocity2D(), new Rotation2D(), new Acceleration2D(), new Torque2D()]
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/movable/prefabs/movable3d.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GlobalTransform3D, Orientation3D, Position3D, Scale3D } from '../../transform/index.js'
import { createTransform3D, GlobalTransform3D, Orientation3D, Position3D, Scale3D } from '../../transform/index.js'
import { Velocity3D, Rotation3D, Acceleration3D, Torque3D } from '../components/index.js'

/**
* @returns {[Position3D,Orientation3D, Scale3D,GlobalTransform3D,Velocity3D,Rotation3D,Acceleration3D,Torque3D]}
*/
export function createMovable3D() {
return [new Position3D(), new Orientation3D(), new Scale3D(), new GlobalTransform3D(), new Velocity3D(), new Rotation3D(), new Acceleration3D(), new Torque3D()]
return [...createTransform3D(), new Velocity3D(), new Rotation3D(), new Acceleration3D(), new Torque3D()]
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/physics/systems/physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CollisionManifold, Contacts } from '../../narrowphase/index.js'
import { PhysicsSettings } from '../settings.js'
import { Collider2D } from '../components/index.js'
import { Orientation2D, Position2D, Scale2D } from '../../transform/index.js'
import { Rotary } from '../../math/index.js'

/**
* @param {World} world
Expand All @@ -15,7 +16,7 @@ export function updateBodies(world) {
Collider2D.update(
shape,
position,
orientation.value,
Rotary.toAngle(orientation),
scale
)
})
Expand Down
4 changes: 2 additions & 2 deletions src/render-core/prefabs/camera2d.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GlobalTransform2D, Orientation2D, Position2D, Scale2D } from '../../transform/index.js'
import { createTransform2D, GlobalTransform2D, Orientation2D, Position2D, Scale2D } from '../../transform/index.js'
import { Camera, RenderLists2D } from '../components/index.js'

/**
Expand All @@ -10,5 +10,5 @@ import { Camera, RenderLists2D } from '../components/index.js'
* @returns {[Position2D,Orientation2D,Scale2D,GlobalTransform2D,Camera,RenderLists2D]}
*/
export function createCamera2D(x = 0, y = 0, a = 0, sx = 1, sy = 1) {
return [new Position2D(x, y), new Orientation2D(a), new Scale2D(sx, sy), new GlobalTransform2D(), new Camera(), new RenderLists2D()]
return [...createTransform2D(x, y, a, sx, sy), new Camera(), new RenderLists2D()]
}
22 changes: 8 additions & 14 deletions src/render-core/prefabs/camera3d.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Quaternion } from '../../math/index.js'
import { Orientation3D, Position3D, Scale3D, GlobalTransform3D } from '../../transform/index.js'
import { Orientation3D, Position3D, Scale3D, GlobalTransform3D, createTransform3D } from '../../transform/index.js'
import { Camera, RenderLists3D } from '../components/index.js'
import { } from '../plugins/index.js'

Expand All @@ -16,23 +15,18 @@ import { } from '../plugins/index.js'
* @returns {[Position3D,Orientation3D,Scale3D,GlobalTransform3D,Camera,RenderLists3D]}
*/
export function createCamera3D(
x = 0,
y = 0,
x = 0,
y = 0,
z = 0,
ox = 0,
oy = 0,
ox = 0,
oy = 0,
oz = 0,
sx = 1,
sy = 1,
sx = 1,
sy = 1,
sz = 1
) {
const quaternion = Quaternion.fromEuler(ox, oy, oz)

return [
new Position3D(x, y, z),
new Orientation3D().copy(quaternion),
new Scale3D(sx, sy, sz),
new GlobalTransform3D(),
...createTransform3D(x, y, z, ox, oy, oz, sx, sy, sz),
new Camera(),
new RenderLists3D()
]
Expand Down
4 changes: 2 additions & 2 deletions src/transform/components/2d/orientation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Angle } from '../../../math/index.js'
import { Rotary } from '../../../math/index.js'

export class Orientation2D extends Angle{}
export class Orientation2D extends Rotary {}
2 changes: 1 addition & 1 deletion src/transform/prefabs/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createTransform2D(
) {
return [
new Position2D(dx, dy),
new Orientation2D(a),
new Orientation2D(Math.cos(a), Math.sin(a)),
new Scale2D(sx, sy),
new GlobalTransform2D()
]
Expand Down
2 changes: 1 addition & 1 deletion src/transform/systems/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function transformRemote2D(world) {
position.copy(entity[0]).add(offPosition)
}
if (remote.copyOrientation) {
orientation.value = entity[1].value + Rotary.toAngle(offOrientation)
orientation.copy(Rotary.multiply(entity[1], offOrientation))
}
if (remote.copyScale) {
scale.copy(entity[2]).multiply(offScale)
Expand Down
4 changes: 2 additions & 2 deletions src/transform/systems/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, has, Query, without, World } from '../../ecs/index.js'
import { Children, Parent } from '../../hierarchy/index.js'
import { Affine2, Affine3, Rotary } from '../../math/index.js'
import { Affine2, Affine3 } from '../../math/index.js'
import { RelationshipQuery } from '../../relationship/index.js'
import { Position2D, Orientation2D, Scale2D, GlobalTransform2D, Position3D, Orientation3D, Scale3D, GlobalTransform3D } from '../components/index.js'

Expand All @@ -11,7 +11,7 @@ export function synctransform2D(world) {
const query = new Query(world, [Position2D, Orientation2D, Scale2D, GlobalTransform2D])

query.each(([position, orientation, scale, transform]) => {
transform.compose(position, Rotary.fromAngle(orientation.value), scale)
transform.compose(position, orientation, scale)
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/tween/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
TweenRepeat,
Tween
} from './components/index.js'
import { Vector2, Quaternion, Vector3, Angle } from '../math/index.js'
import { Vector2, Quaternion, Vector3, Rotary } from '../math/index.js'
import { generateTweenFlipSystem, generateTweenRepeatTween, generateTweenTimerSystem, generateTweenUpdateSystem } from './systems/index.js'
import { Orientation2D, Orientation3D, Position2D, Position3D, Scale2D, Scale3D } from '../transform/index.js'
import { typeidGeneric } from '../reflect/index.js'
Expand All @@ -36,7 +36,7 @@ export class DefaultTweenPlugin extends Plugin {
.registerPlugin(new TweenPlugin({
component: Orientation2D,
tween: Orientation2DTween,
interpolation: Angle.lerp
interpolation: Rotary.slerp
}))
.registerPlugin(new TweenPlugin({
component: Orientation3D,
Expand Down