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

fixture.computeDistance gives incorrect results for CircleShape #99

Closed
drrnbrns opened this issue Dec 17, 2024 · 6 comments · Fixed by #100
Closed

fixture.computeDistance gives incorrect results for CircleShape #99

drrnbrns opened this issue Dec 17, 2024 · 6 comments · Fixed by #100

Comments

@drrnbrns
Copy link
Contributor

drrnbrns commented Dec 17, 2024

Using version 0.14.0 of forge2d, CircleShape.computeDistanceToOut isn't returning the right values. Note: I've included a fix that works well below.

This code logs distance and normals for both CircleShape and PolygonShape (the latter works fine and is just for comparison). If you draw or think about the locations, you'll see that the circleDistance reported is way off. Flip CircleShape to CircleShapeWithFix and you'll see correct values.

Expand for test case

Test case

import 'dart:math';
import 'package:forge2d/forge2d.dart';

void testFixtures() {
  final world = World();
  final body = world.createBody(BodyDef(
    angle: 0.1,
    position: Vector2(1, 2),
  ));

  final normal = Vector2.zero();
  final testPoint = Vector2(10, 7);

  // Flip CircleShape to CircleShapeWithFix to see the fix.
  final circleShape = CircleShape()
    ..radius = 2
    ..position.x = 3
    ..position.y = 4;
  final circleFixture = body.createFixtureFromShape(circleShape);
  final circleDistance = circleFixture.computeDistance(testPoint, 0, normal);
  print("circleDistance $circleDistance normal $normal");

  final polygonShape = PolygonShape()..setAsBoxXY(2, 3);
  final polygonFixture = body.createFixtureFromShape(polygonShape);
  final polygonDistance = polygonFixture.computeDistance(testPoint, 0, normal);
  print("polygonDistance $polygonDistance normal $normal");
}
Expand for provided working code

Working code for CircleShape.computeDistanceToOut

Provided as a class, but the intent is to simply use the code in computeDistanceToOut. In my app, I have a large number of different cases that all work well with this fixed code.

// CircleShapeWithFix only exists to demonstrate the fixed code in computeDistanceToOut.
class CircleShapeWithFix extends CircleShape {
  @override
  double computeDistanceToOut(
    Transform xf,
    Vector2 p,
    int childIndex,
    Vector2 normalOut,
  ) {
    final q = xf.q;
    final tp = xf.p;
    final dx = -(q.cos * position.x - q.sin * position.y + tp.x - p.x);
    final dy = -(q.sin * position.x + q.cos * position.y + tp.y - p.y);
    final d1 = sqrt(dx * dx + dy * dy);
    normalOut.x = dx * 1 / d1;
    normalOut.y = dy * 1 / d1;
    return d1 - radius;
  }

  @override
  Shape clone() => CircleShapeWithFix()
    ..radius = radius
    ..position.setFrom(position);
}

@spydon
Copy link
Member

spydon commented Dec 17, 2024

Thanks for the fix, could you provide it as a PR and add some test cases?

@drrnbrns
Copy link
Contributor Author

I'll give it a shot, but I'm not too familiar with the internals of this project. It will actually be my first PR on github. Do you have any guidance about where tests would go, and is there a document I should read first?

@spydon
Copy link
Member

spydon commented Dec 17, 2024

You can read this one if you want:
https://github.com/flame-engine/flame/blob/main/CONTRIBUTING.md
The gist is basically that you should install melos and run melos bootstrap first.

The test can go in here (there are sadly not that many tests yet, but we try to at least make them for bug fixes so that we don't get regressions):
https://github.com/flame-engine/forge2d/tree/main/packages/forge2d/test

And feel free to ask questions if you get stuck. :)

@drrnbrns
Copy link
Contributor Author

Thanks, I'll read through that contributing doc. You have the same link twice though.

@spydon
Copy link
Member

spydon commented Dec 17, 2024

Thanks, I'll read through that contributing doc. You have the same link twice though.

Fixed!

@drrnbrns
Copy link
Contributor Author

I created a PR (#100) but I'm not sure what happens next. I can't assign anyone.

The github docs suggest that I can't assign it since I don't have write access, so I'm not clear on what normally happens to get it assigned.

spydon pushed a commit that referenced this issue Dec 18, 2024
# Description

<!-- Provide a description of what this PR is doing. 
If you're modifying existing behavior, describe the existing behavior,
how this PR is changing it,
and what motivated the change. If this is a breaking change, specify
explicitly which APIs have been
changed. -->

## Checklist

<!-- Before you create this PR confirm that it meets all requirements
listed below by checking the
relevant checkboxes (`[x]`). This will ensure a smooth and quick review
process. -->

- [x] The title of my PR starts with a [Conventional Commit] prefix
(`fix:`, `feat:`, `docs:` etc).
- [x] I have read the [Contributor Guide] and followed the process
outlined for submitting PRs.
- [x] I have updated/added tests for ALL new/updated/fixed
functionality.
- [ ] (N/A) I have updated/added relevant documentation in `docs` and
added dartdoc comments with `///`.
- [ ] (N/A) I have updated/added relevant examples in `examples`.

## Breaking Change

<!-- Does your PR require Flame users to manually update their apps to
accommodate your change?

If the PR is a breaking change this should be indicated with suffix "!"
(for example, `feat!:`, `fix!:`). See [Conventional Commit] for details.
-->

- [ ] Yes, this is a breaking change.
- [x] No, this is *not* a breaking change.

## Related Issues

<!-- Provide a list of issues related to this PR from the [issue
database].
Indicate which of these issues are resolved or fixed by this PR, i.e.
Fixes #xxxx* !-->
Fixes #99.

<!-- Links -->
[issue database]: https://github.com/flame-engine/flame/issues
[Contributor Guide]:
https://github.com/flame-engine/flame/blob/main/CONTRIBUTING.md
[Flame Style Guide]:
https://github.com/flame-engine/flame/blob/main/STYLEGUIDE.md
[Conventional Commit]: https://conventionalcommits.org

Co-authored-by: drrnbrns <[email protected]>
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 a pull request may close this issue.

2 participants