I have a "ball" at
x = 10
y = 15
next tick it's
x = 15
y = 20
How do I figure out what y position the ball will be at x = 30?
I can make it into a vector .. but then what?
JavaScript / NodeJs.
I have a "ball" at
x = 10
y = 15
next tick it's
x = 15
y = 20
How do I figure out what y position the ball will be at x = 30?
I can make it into a vector .. but then what?
JavaScript / NodeJs.
The Rapier is my love boat
~lowsec smallscale pvp 'n stuff~
Assuming constant velocity? Or gravity being 9.8 m/s^2 down the y-axis?
If just constant, you want the derivative of the function, i.e. the gradient of the line, which is first found by solving the simultaneous equation for those two known points.
y = xa + b.
15 = 10a + b. 20 = 15a + b.
15 - 10a = b = 20 - 15a.
-10a = 5 - 15a.
0 = 5-5a.
5a = 5.
a = 1.
15 = 10(1) + b.
b = 15 - 10(1).
b = 5.
20 = 15(1) + 5. Checks out.
y = 30(1) + 5 = 35.
Another way might be to say take the second position from the first to define movement per tick, then divide the distance to target x by movement-in-x-per-tick, to give ticks it'll take. Then just add that many movement-in-x-per-tick by that tick count.
E.g. 15-10 is 5. That's x per tick.
30-15 is 15 change in x total. /5 x-per-tick is 3 ticks.
20-15 is also 5 y per tick.
3x5 is 15 change in y after 3 ticks. 20+15 puts y at 35.
Currently reading your edits, but posting anyway
_____________
Velocity does not matter, as I don't care when it's at point y.
No gravity.
It's a bot for a pong game.
Ok, I know the current position and the last position.
And I know the X where I want to intercept the ball.Code:var currentPoint = {x: gameData.ball.position.x, y: gameData.ball.position.y} var diff = { x: currentPoint.x - oldPoint.x, y: currentPoint.y - oldPoint.y }
I would need to figure out at what rate is y rising for every x, right?Code:var paddlex = gameData.paddles[socket.id].position.x;
As we know target X and current X I know the distance:Code:var yrate = diff.y / diff.x;
Is bla now the difference between current y and target y?Code:var bla = yrate * (paddlex - currentPoint.x)
currentPoint + bla would be estimated target y?
The Rapier is my love boat
~lowsec smallscale pvp 'n stuff~
Ah, nice. Implementet it your way and it works \o/
Neato.Code:function foo () { var currentPoint = {x: gameData.ball.position.x, y: gameData.ball.position.y} var diff = { x: currentPoint.x - oldPoint.x, y: currentPoint.y - oldPoint.y } var targetx = gameData.paddles[socket.id].position.x; var xchange = targetx - currentPoint.x; var xticks = xchange / diff.x; var ychange = xticks * diff.y; console.log("estimatey: " + currentPoint.y + bla); oldPoint = currentPoint; return currentPoint.y + ychange; }
I appreciate it
The Rapier is my love boat
~lowsec smallscale pvp 'n stuff~
A single bounce will simply mirror the ball the distance it would otherwise be past the boundary in one axis that far below it. E.g. start 3 below, moves 5 along and 5 up ignoring bounce, that's 2 over, bounce actually puts it 2 below at the same 5-along horizontal coordinate.
Again, assuming a single bounce/boundary is encountered. iirc. I think this also gives you perfect reflection & momentum conservation, no worries about simulation with collision detection accuracy (i.e. no need to define an area or volume then check the ball one intersects, just a half-plane and a point).
Oh shit fam u right ))
That works.
Thank you.
The Rapier is my love boat
~lowsec smallscale pvp 'n stuff~
Np, I think it holds so long as you're doing regular only-reflecting-paddles pong, and not air-hockey style where the angles could vary & the puck could bounce off opposing sides within 1 tick. Even then it might just be some some modulo of the unbounded height by the table height to determine current direction sign & which side to offset from.
Bookmarks