hate these ads?, log in or register to hide them
Page 123 of 123 FirstFirst ... 2373113120121122123
Results 2,441 to 2,449 of 2449

Thread: <xml><thread isTerrible="true" isUnderstandable="false" /></xml> - Coding help thread

  1. #2441
    root's Avatar
    Join Date
    April 26, 2011
    Location
    The Camel Empire
    Posts
    3,018
    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~

  2. #2442
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    12,207
    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.
    Last edited by Daneel Trevize; February 18 2018 at 01:52:20 PM.
    Quote Originally Posted by QuackBot View Post
    Idk about that, and i'm fucking stupid.

  3. #2443
    root's Avatar
    Join Date
    April 26, 2011
    Location
    The Camel Empire
    Posts
    3,018
    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.
    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
        }
    And I know the X where I want to intercept the ball.

    Code:
    var paddlex = gameData.paddles[socket.id].position.x;
    I would need to figure out at what rate is y rising for every x, right?

    Code:
    var yrate = diff.y / diff.x;
    As we know target X and current X I know the distance:

    Code:
    var bla = yrate * (paddlex - currentPoint.x)
    Is bla now the difference between current y and target y?
    currentPoint + bla would be estimated target y?
    The Rapier is my love boat
    ~lowsec smallscale pvp 'n stuff~

  4. #2444
    root's Avatar
    Join Date
    April 26, 2011
    Location
    The Camel Empire
    Posts
    3,018
    Ah, nice. Implementet it your way and it works \o/

    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;
    }
    Neato.
    I appreciate it
    The Rapier is my love boat
    ~lowsec smallscale pvp 'n stuff~

  5. #2445
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    12,207
    Just don't forget to test/case for zero difference (div 0 error), or negative difference from moving in the opposite direction.
    Quote Originally Posted by QuackBot View Post
    Idk about that, and i'm fucking stupid.

  6. #2446
    root's Avatar
    Join Date
    April 26, 2011
    Location
    The Camel Empire
    Posts
    3,018
    Quote Originally Posted by Daneel Trevize View Post
    Just don't forget to test/case for zero difference (div 0 error), or negative difference from moving in the opposite direction.
    I think I will put the paddle in the middle when the ball is moving away from me.
    There is also the case on how it bounces off the wall .. but that will be too complex for now.
    The Rapier is my love boat
    ~lowsec smallscale pvp 'n stuff~

  7. #2447
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    12,207
    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).
    Last edited by Daneel Trevize; February 18 2018 at 03:50:44 PM.
    Quote Originally Posted by QuackBot View Post
    Idk about that, and i'm fucking stupid.

  8. #2448
    root's Avatar
    Join Date
    April 26, 2011
    Location
    The Camel Empire
    Posts
    3,018
    Oh shit fam u right ))
    That works.
    Thank you.
    The Rapier is my love boat
    ~lowsec smallscale pvp 'n stuff~

  9. #2449
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    12,207
    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.
    Quote Originally Posted by QuackBot View Post
    Idk about that, and i'm fucking stupid.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •