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

Add support for frictionGravity #402

Merged
merged 10 commits into from
Aug 17, 2022
Merged

Conversation

chnicoloso
Copy link
Contributor

Howdy - updating the worker api to enable setting the world's frictionGravity. This is a new property that was just added to cannon-es (pmndrs/cannon-es#156) to address schteppe/cannon.js#224 and allow friction to be optionally applied to worlds without gravity.

@vercel
Copy link

vercel bot commented Aug 12, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
use-cannon ✅ Ready (Inspect) Visit Preview Aug 17, 2022 at 9:36PM (UTC)

@@ -40,7 +40,7 @@
"@types/three": "^0.139.0",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
"cannon-es": "^0.19.0",
"cannon-es": "^0.20.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that cannon-es is listed as a devDependency but it reads to me like it should be a dependency so I'm wondering what I'm missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannon-es is bundled into the worker as part of our build process and does not need to be installed separately by the people using use-cannon.

@chnicoloso
Copy link
Contributor Author

Hi @isaac-mason, can I borrow your eagle eyes for another small one? 😄
Otherwise, hi @bjornstar you also come up as a recent collaborator 👀

@Glavin001
Copy link
Contributor

@chnicoloso out of curiosity, is the demo you shared open-source (and using react-xr)? Look interesting, would love to take a look!

Before After
ezgif com-gif-maker (1) ezgif com-gif-maker

@@ -65,7 +73,7 @@ export class CannonWorkerAPI extends EventEmitter {
quaternions: Float32Array
}

private config: Required<CannonWorkerProps>
private config: Required<Omit<CannonWorkerProps, 'frictionGravity'>> & { frictionGravity?: WorldProps['frictionGravity'] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't make an exception for this, we send over the config with default values specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to ideas on expressing that this an optional value without adding this exception - I admit, it is pretty ugly!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal interface, we don't have any optional properties here. If an acceptable value is undefined, that is okay.

@@ -42,6 +42,14 @@ export class CannonWorkerAPI extends EventEmitter {
this.postMessage({ op: 'setGravity', props: value })
}

get frictionGravity(): Triplet | undefined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of our interface is optional, it should always return a Triplet.

Copy link
Member

@isaac-mason isaac-mason Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, a possible value for frictionGravity is undefined.

https://pmndrs.github.io/cannon-es/docs/classes/World.html#frictionGravity

Gravity to use when approximating the friction max force (mu * mass * gravity). If undefined, global gravity will be used. Use to enable friction in a World with a null gravity vector (no gravity).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks Isaac! @bjornstar I could return an empty array here if always returning a value is important - but keeping the worker api signature consistent with the World interface seems preferable to me.

get frictionGravity(): Triplet | undefined {
return this.config.frictionGravity;
}
set frictionGravity(value: Triplet | undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an optional value here.

Also, could you please keep the ordering of the properties alphabetical, this should go above gravity, I'm not sure why our linter is not complaining about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the linter didn't run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify what you mean by it not being optional here? My intention here was to get across that the user may define frictionGravity, and they may also reset it back to undefined.

@@ -75,6 +83,7 @@ export class CannonWorkerAPI extends EventEmitter {
broadphase = 'Naive',
defaultContactMaterial = { contactEquationStiffness: 1e6 },
gravity = [0, -9.81, 0],
frictionGravity,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify a default value and keep the ordering alphabetical.

Copy link
Contributor Author

@chnicoloso chnicoloso Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye aye on the ordering! On providing a default: the expected default on cannon-es for this property is undefined. Would you like me to assign frictionGravity = undefined here to at least make it explicit to readers? Would a little comment be welcomed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please assign undefined as the default if that's what it is.

@chnicoloso
Copy link
Contributor Author

@Glavin001 thanks for the interest! I am not using react-xr. I don't have anything up yet but I'll let you know when I do!

@bjornstar
Copy link
Member

This looks good, just some minor stylistic issues from my side and we should be good to go.

@chnicoloso
Copy link
Contributor Author

Thank you @bjornstar! Updated

@@ -65,7 +73,7 @@ export class CannonWorkerAPI extends EventEmitter {
quaternions: Float32Array
}

private config: Required<CannonWorkerProps>
private config: Required<Omit<CannonWorkerProps, 'frictionGravity'>> & { frictionGravity?: WorldProps['frictionGravity'] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to change.

Suggested change
private config: Required<Omit<CannonWorkerProps, 'frictionGravity'>> & { frictionGravity?: WorldProps['frictionGravity'] }
private config: Required<CannonWorkerProps>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think it does! Otherwise, TS would yell about this config assignment: https://github.com/pmndrs/use-cannon/pull/402/files#diff-e3f4dac018aec1d1b23bbbb9ea7d1e0edbdc2dcfee7f35d088d8e145754add61R101, since config would be typed with frictionGravity as required.

Let me know what you think I'm missing!

@bjornstar
Copy link
Member

I ended up using null internally instead of undefined, it made things much more straight-forward.

@bjornstar bjornstar merged commit a1be52b into pmndrs:master Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants