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 Tear constraint example #318

Merged
merged 1 commit into from
Jan 8, 2022

Conversation

Glavin001
Copy link
Contributor

@Glavin001 Glavin001 commented Dec 23, 2021

Add maxMultipler threshold to constraints.

Inspired by https://github.com/pmndrs/cannon-es/blob/6f11e268d5e196f7f4f5908dbf60ae6365bd6ab6/examples/tear.html

  • Tear constraint
    • Add maxMultiplier option to all constraints, which disables constraint when threshold is exceeded
      • Only add postStep listener if maxMultiplier set / not undefined
    • Inspired by: https://pmndrs.github.io/cannon-es/examples/tear
    • Update docs
      • TypeScript interface
      • README
    • Merged Chain & Tear into a single example/demo
      • Single file that demonstrate a feature. We should be able to take that file put it in a sandbox with minimal setup.
      • Includes pointer position (from original Chain)
      • Includes multiple maxMultiplier values
        • Color of chain indicates the value (0 = blue/easily break, 1500 = red/harder to break, white = undefined/won't break)
      • Click to reset torn chains / re-enable constraints

Demo

https://use-cannon-git-fork-glavin001-feat-constraint-max-e97738-pmndrs.vercel.app/#/demo/Chain

Initially:

image

After breaking some chain constraints:

image

@vercel
Copy link

vercel bot commented Dec 23, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pmndrs/use-cannon/6CrKqrLJVz6F8vK9yBxS1wtGiUBC
✅ Preview: https://use-cannon-git-fork-glavin001-feat-constraint-max-e97738-pmndrs.vercel.app

src/hooks.ts Show resolved Hide resolved
src/worker.js Outdated Show resolved Hide resolved
src/worker.js Outdated Show resolved Hide resolved
@Glavin001
Copy link
Contributor Author

Glavin001 commented Dec 24, 2021

@bjornstar : I've commented on the code above with questions I'd appreciate your feedback on so I can implement it in a way you're happy with. Hope this helps. Thanks!

src/worker.js Outdated Show resolved Hide resolved
@Glavin001
Copy link
Contributor Author

@bjornstar Ready for final review 🎉

src/worker.js Outdated
@@ -331,12 +332,29 @@ self.onmessage = (e) => {
}
constraint.uuid = uuid
state.world.addConstraint(constraint)

if (!isNaN(maxMultiplier)) {
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 use isNaN

Copy link
Contributor Author

@Glavin001 Glavin001 Jan 6, 2022

Choose a reason for hiding this comment

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

OK. Would a truthy check be satisfactory?

Suggested change
if (!isNaN(maxMultiplier)) {
if (maxMultiplier) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The above wouldn't support maxMultiplier being 0, this may be more appropriate:

Suggested change
if (!isNaN(maxMultiplier)) {
if (maxMultiplier !== undefined) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

<spotLight position={[10, 10, 10]} angle={0.3} penumbra={1} intensity={1} castShadow />
<Physics gravity={[0, -40, 0]} allowSleep={false}>
<Handle radius={0.5}>
<Projectile radius={2.0} />
Copy link
Member

@bjornstar bjornstar Jan 6, 2022

Choose a reason for hiding this comment

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

I don't think the Projectile should be inside of the chain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do. This was to ensure the Projectile lines up the shot with the Handle and the Chain Links below 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.

Removed projectile entirely in favour of your suggestion:

Can we drag one chain through several other chains that tear?

@bjornstar
Copy link
Member

I'd really prefer not to have cross dependencies between demos. The hallmark of demos is that they are a single file that demonstrate a feature. We should be able to take that file put it in a sandbox with minimal setup.

If the two are so similar that they share files, they should be in the same file with some form of interaction. Can we drag one chain through several other chains that tear? How about clicking to reset the torn chains?

@Glavin001
Copy link
Contributor Author

@bjornstar : Pushed new demo/example and other fixes based on your feedback above. Ready for review again. Thanks!

@bjornstar bjornstar merged commit 7cfec9d into pmndrs:master Jan 8, 2022
@bjornstar
Copy link
Member

Cool, I think this turned out quite well. Thanks for the contribution!

@bjornstar
Copy link
Member

Released in v4.5.0

@Glavin001
Copy link
Contributor Author

Thanks for merging, @bjornstar !

Next up is #317 (comment)
All ready for you 🙂🎉

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.

2 participants