You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
constball=Bodies.circle(0,0,10);constrectangles=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){consttarget=rectangles[rectangles.indexOf(pair.bodyB)+1];constgravity=engine.gravity.y;constinitialAngle=60;constyOffset=ball.position.y-target.position.y;constangle=initialAngle*(Math.PI/180);constdistance=Vector.magnitude(Vector.sub(ball.position,target.position));constinitialVelocity=(1/Math.cos(angle))*Math.sqrt((0.5*gravity*Math.pow(distance,2))/(distance*Math.tan(angle)+yOffset),);constvelocity={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
@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).
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:
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 oneHere is codesandbox example:
https://codesandbox.io/p/sandbox/relaxed-shape-9wy3kt
The text was updated successfully, but these errors were encountered: