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

Scale fluctuates when just grabbing a block on top of the scale with keyboard #414

Open
Matthew-Moore240 opened this issue Sep 24, 2024 · 1 comment
Labels

Comments

@Matthew-Moore240
Copy link

Test device
Win 10 Desktop

Operating System
Win 10

Browser
Chrome 129.0.6668.59

Problem description
For phetsims/qa#1141 Scale weight either fluctuates rapidly or switches between two different weights just grabbing the block and no other inputs. I know there are other issues regarding the weight measurements on scales, apologies if this is already being addressed. This happens in both Buoyancy: basics and Buoyancy. It does not happen in Density.

Steps to reproduce

  1. Put block on scale
  2. Tab to block
  3. Use Space to grab

Visuals

Buoyancy_.Basics.-.mp4
Troubleshooting information: !!!!! DO NOT EDIT !!!!! Name: ‪Buoyancy: Basics‬ URL: https://phet-dev.colorado.edu/html/buoyancy-basics/1.2.0-rc.1/phet/buoyancy-basics_all_phet.html Version: 1.2.0-rc.1 2024-09-10 21:36:52 UTC Features missing: applicationcache, applicationcache, touch Flags: pixelRatioScaling User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Language: en-US Window: 2048x1026 Pixel Ratio: 1.25/1 WebGL: WebGL 1.0 (OpenGL ES 2.0 Chromium) GLSL: WebGL GLSL ES 1.0 (OpenGL ES GLSL ES 1.0 Chromium) Vendor: WebKit (WebKit WebGL) Vertex: attribs: 16 varying: 30 uniform: 4095 Texture: size: 16384 imageUnits: 16 (vertex: 16, combined: 32) Max viewport: 32767x32767 OES_texture_float: true Dependencies JSON: {}
@Matthew-Moore240 Matthew-Moore240 added the type:bug Something isn't working label Sep 24, 2024
@samreid
Copy link
Member

samreid commented Oct 2, 2024

I tried LockConstraint, but it had the same bug on the scale and very buggy collision behavior:

Subject: [PATCH] comment
---
Index: js/common/model/PhysicsEngine.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/common/model/PhysicsEngine.ts b/js/common/model/PhysicsEngine.ts
--- a/js/common/model/PhysicsEngine.ts	(revision 234443020a88a811a2c932ac0fb83d4d6ba2a85d)
+++ b/js/common/model/PhysicsEngine.ts	(date 1727894936487)
@@ -53,7 +53,7 @@
   private readonly world: p2.World;
 
   // Maps {number} body.id => {p2.RevoluteConstraint}
-  private readonly pointerConstraintMap: Record<number, p2.RevoluteConstraint>;
+  private readonly pointerConstraintMap: Record<number, p2.LockConstraint>;
 
   // Maps {number} body.id => {p2.Body}. Contains bodies that are empty, and specifically used for
   // pointer constraints (so they can be positioned to where the pointer is).
@@ -367,38 +367,42 @@
     assert && assert( !this.pointerConstraintMap.hasOwnProperty( body.id ), 'there is already a pointer constraint for this body' );
     assert && assert( !this.nullBodyMap.hasOwnProperty( body.id ), 'there is already a null body for this body' );
 
-    // Create an empty body used for the constraint (we don't want it intersecting). It will just be used for applying
-    // the effects of this constraint.
-    const nullBody = new p2.Body();
+    // Create an empty body used for the constraint
+    const nullBody = new p2.Body( {
+      mass: 0, // Make sure the nullBody is static
+      position: [ position.x * SIZE_SCALE, position.y * SIZE_SCALE ]
+    } );
     this.nullBodyMap[ body.id ] = nullBody;
 
-    const globalPoint = PhysicsEngine.vectorToP2( position );
-    const localPoint = p2.vec2.create();
-    body.toLocalFrame( localPoint, globalPoint );
     this.world.addBody( nullBody );
 
     body.wakeUp();
 
-    const pointerConstraint = new p2.RevoluteConstraint( nullBody, body, {
-      localPivotA: globalPoint,
-      localPivotB: localPoint,
-      maxForce: DensityBuoyancyCommonQueryParameters.p2PointerMassForce * body.mass + DensityBuoyancyCommonQueryParameters.p2PointerBaseForce
+    const lockConstraint = new p2.LockConstraint( nullBody, body, {
+      collideConnected: false
+      // Optionally, you can add stiffness and damping if supported
     } );
-    this.pointerConstraintMap[ body.id ] = pointerConstraint;
-    this.world.addConstraint( pointerConstraint );
+    this.pointerConstraintMap[ body.id ] = lockConstraint;
+    this.world.addConstraint( lockConstraint );
   }
 
   /**
    * Updates a pointer constraint so that the body will essentially be dragged to the new position.
    */
   public updatePointerConstraint( body: PhysicsEngineBody, position: Vector2 ): void {
-    const pointerConstraint = this.pointerConstraintMap[ body.id ];
-    assert && assert( pointerConstraint, `pointer constraint expected for physics body #${body.id}` );
+    const lockConstraint = this.pointerConstraintMap[ body.id ];
+    assert && assert( lockConstraint, `pointer constraint expected for physics body #${body.id}` );
+
+    const nullBody = this.nullBodyMap[ body.id ];
+    assert && assert( nullBody, `null body expected for physics body #${body.id}` );
 
-    // @ts-expect-error it should have pivotA...
-    p2.vec2.copy( pointerConstraint.pivotA, PhysicsEngine.vectorToP2( position ) );
-    pointerConstraint.bodyA.wakeUp();
-    pointerConstraint.bodyB.wakeUp();
+    // Update the position of the nullBody to the new pointer position
+    nullBody.position[ 0 ] = position.x * SIZE_SCALE;
+    nullBody.position[ 1 ] = position.y * SIZE_SCALE;
+
+    // Wake up both bodies to ensure the constraint is processed
+    body.wakeUp();
+    nullBody.wakeUp();
   }
 
   /**
@@ -407,9 +411,9 @@
   public removePointerConstraint( body: PhysicsEngineBody ): void {
     const nullBody = this.nullBodyMap[ body.id ];
     assert && assert( nullBody, `cannot remove physics body #${body.id}` );
-    const pointerConstraint = this.pointerConstraintMap[ body.id ];
+    const lockConstraint = this.pointerConstraintMap[ body.id ];
 
-    this.world.removeConstraint( pointerConstraint );
+    this.world.removeConstraint( lockConstraint );
     this.world.removeBody( nullBody );
 
     delete this.nullBodyMap[ body.id ];

@zepumph zepumph added the dev:help-wanted Extra attention is needed label Oct 2, 2024
@zepumph zepumph removed their assignment Oct 2, 2024
@zepumph zepumph removed the dev:help-wanted Extra attention is needed label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants