Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change area effector to rectangular coordinates #479

Merged
merged 2 commits into from
Oct 30, 2024
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
2 changes: 1 addition & 1 deletion examples/physicsfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ add([
pos(20, 150),
rect(50, 300),
area(),
areaEffector({ forceAngle: -90, forceMagnitude: 150 }),
areaEffector({ force: UP.scale(150) }),
{
draw() {
drawPolygon({
Expand Down
45 changes: 20 additions & 25 deletions src/components/physics/effectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,30 @@ export function surfaceEffector(

export type AreaEffectorCompOpt = {
useGlobalAngle?: boolean;
forceAngle: number;
forceMagnitude: number;
forceVariation?: number;
force: Vec2;
linearDrag?: number;
// angularDrag?: number;
};

export interface AreaEffectorComp extends Comp {
useGlobalAngle: boolean;
forceAngle: number;
forceMagnitude: number;
forceVariation: number;
linearDrag?: number;
// angularDrag?: number;
force: Vec2;
linearDrag: number;
}

export function areaEffector(opts: AreaEffectorCompOpt): AreaEffectorComp {
return {
id: "areaEffector",
require: ["area"],
useGlobalAngle: opts.useGlobalAngle || false,
forceAngle: opts.forceAngle,
forceMagnitude: opts.forceMagnitude,
forceVariation: opts.forceVariation ?? 0,
force: opts.force,
linearDrag: opts.linearDrag ?? 0,
// angularDrag: opts.angularDrag ?? 0,
useGlobalAngle: opts.useGlobalAngle ?? true,
add(this: GameObj<AreaComp | AreaEffectorComp>) {
this.onCollideUpdate("body", (obj, col) => {
const dir = Vec2.fromAngle(this.forceAngle);
const force = dir.scale(this.forceMagnitude);
obj.addForce(force);
this.onCollideUpdate("body", obj => {
obj.addForce(
this.useGlobalAngle
? this.force
: this.force.rotate(this.transform.getRotation()),
);
if (this.linearDrag) {
obj.addForce(obj.vel.scale(-this.linearDrag));
}
Expand All @@ -82,28 +75,23 @@ export type ForceMode = "constant" | "inverseLinear" | "inverseSquared";

export type PointEffectorCompOpt = {
forceMagnitude: number;
forceVariation: number;
distanceScale?: number;
forceMode?: ForceMode;
linearDrag?: number;
// angularDrag?: number;
};

export interface PointEffectorComp extends Comp {
forceMagnitude: number;
forceVariation: number;
distanceScale: number;
forceMode: ForceMode;
linearDrag: number;
// angularDrag: number;
}

export function pointEffector(opts: PointEffectorCompOpt): PointEffectorComp {
return {
id: "pointEffector",
require: ["area", "pos"],
forceMagnitude: opts.forceMagnitude,
forceVariation: opts.forceVariation ?? 0,
distanceScale: opts.distanceScale ?? 1,
forceMode: opts.forceMode || "inverseLinear",
linearDrag: opts.linearDrag ?? 0,
Expand Down Expand Up @@ -132,20 +120,27 @@ export function pointEffector(opts: PointEffectorCompOpt): PointEffectorComp {

export type ConstantForceCompOpt = {
force?: Vec2;
useGlobalAngle?: boolean;
};

export interface ConstantForceComp extends Comp {
force?: Vec2;
force: Vec2 | undefined;
useGlobalAngle: boolean;
}

export function constantForce(opts: ConstantForceCompOpt): ConstantForceComp {
return {
id: "constantForce",
require: ["body"],
force: opts.force,
useGlobalAngle: opts.useGlobalAngle ?? true,
update(this: GameObj<BodyComp | ConstantForceComp>) {
if (this.force) {
this.addForce(this.force);
this.addForce(
this.useGlobalAngle
? this.force
: this.force.rotate(this.transform.getRotation()),
);
}
},
};
Expand Down