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

Calculate velocity to jump to the target #1247

Closed
ShellarFX opened this issue Oct 3, 2023 · 2 comments
Closed

Calculate velocity to jump to the target #1247

ShellarFX opened this issue Oct 3, 2023 · 2 comments

Comments

@ShellarFX
Copy link

I'm trying to create a ball that will bounce off rectangles and jump to the next rectangle. All i can know is ball's position, next rectangle's position and jump angle. I found similar question at unity forum (https://forum.unity.com/threads/how-to-calculate-force-needed-to-jump-towards-target-point.372288/). I tried to adapt code from Iron-Warrior's solution for my project, but i have problem that my ball constantly jumps strongly over the next rectangle.

Here is the code that i got:

const ball = Bodies.circle(0, 0, 10);

const rectangles = Array(20)
  .fill(0)
  .map((_, i) => Bodies.rectangle(i * 140, 200, 20, 20, { isStatic: true }));

Events.on(engine, 'collisionStart', (event) => {
  event.pairs.forEach((pair) => {
    if (pair.bodyA === ball) {
      const target = rectangles[rectangles.indexOf(pair.bodyB) + 1];

      const gravity = engine.gravity.y;
      const initialAngle = 60;
      const yOffset = ball.position.y - target.position.y;

      const angle = initialAngle * (Math.PI / 180);

      const distance = Vector.magnitude(Vector.sub(ball.position, target.position));

      const initialVelocity =
        (1 / Math.cos(angle)) *
        Math.sqrt(
          (0.5 * gravity * Math.pow(distance, 2)) / (distance * Math.tan(angle) + yOffset),
        );

      const velocity = {
        x: initialVelocity * Math.cos(angle),
        y: -initialVelocity * Math.sin(angle),
      };

      // i believe there is no need to rotate velocity
      // const angleBetweenObjects = Vector.angle(ball.position, target.position);
      // const finalVelocity = Vector.rotate(velocity, angleBetweenObjects);
  
      Body.setVelocity(ball, velocity);        
    }
  });
});

Composite.add(engine.world, [ball, ...rectangles]);

I also found that if i set engine.velocityIterations, for example, to 100, then my ball jumps from the first rectangle to the left edge of the second, and then jumps over the third one

Here is codesandbox example:
https://codesandbox.io/p/sandbox/relaxed-shape-9wy3kt

@ggorlen
Copy link

ggorlen commented Oct 3, 2023

@ShellarFX
Copy link
Author

@ggorlen yeah. There is a great solution on this question on Stack Overflow, thanks to kikon. The main problem was with collisitonStart event (should use collisionActive) and angular velocity of circle (should set it to 0 after setVelocity).

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

No branches or pull requests

2 participants