3
\$\begingroup\$

What I want to happen is for my player to bounce from spring to another. For example take the illustration below:

enter image description here

Here's my code (what I've tried to do so far). This gets called in an update()method

public void springCollision(Box containsSpring) {
        // player object
        Player player = ((Player) this.object);
        // first spring
        Spring boxSpring = containsSpring.getSpring();
        // second spring
        Spring platformSpring = containsSpring.getPartnerPlatform().getSpring();
        // if player collides with first spring
        if (player.getRect().overlaps(boxSpring.getRect())) {
            // distance in x between first & second spring
            float dx = platformSpring.getxPos()
                    + platformSpring.getSprite().getWidth()
                    - boxSpring.getxPos() - player.getSprite().getWidth();
            // distance in y between first & second spring
            float dy = platformSpring.getyPos() - boxSpring.getyPos();
            Vector2 directionToSpring = new Vector2(dx, dy);
            // normalise vector then set player speed 
            player.setxSpeed(directionToSpring.nor().x);
            player.setySpeed(directionToSpring.nor().y + player.getGravity());
        }
    }

What currently happens is that when he jumps onto a spring his jump just continues but in slow motion for some reason. Can anyone see why my algorithm doesn't work? Let me know if the comments are unclear

\$\endgroup\$
  • \$\begingroup\$ It is unclear 1) why you use the normalized distance vector as the speed (plus the gravity) 2) what is the magnitude of the values of the distance and gravity. We often tend to distinguish speed (a single value) from direction (a unit vector) from velocity (the mix of both, a not-necessarily-unit vector, which indicates the speed and the direction), I'm not sure what the speed you set there is really representing. If it is the velocity, you'd want to multiply it by the 'anti-gravity' of the spring and set it as the speed for x and y, then perform normal modification with gravity. \$\endgroup\$ – Vaillancourt Mar 11 '15 at 3:03
3
\$\begingroup\$

To implement a spring I would do this:

  1. On the spring-player collision detection:
    1. Give an impulse to the player toward the desired direction;
    2. Disable the spring;
    3. Let the gravity take care of the rest.
  2. Re-enable the spring after one or more conditions:
    • After a certain delay;
    • After the player is no longer colliding with the spring;
    • After the player is sufficiently away from the spring.
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Seems like a logical approach. How would I give him the 'impulse' though? That's the part I'm struggling on \$\endgroup\$ – eyes enberg Mar 11 '15 at 14:20
  • 1
    \$\begingroup\$ It's the same thing as if you were making your character jump. The only difference, is that when jumping, you give your character an impulse only vertically (-y), while your spring gives also an impulse on the x coordinate. You can work with the difference between the 2 springs as a base, but you'll probably have to tweak it to give the desired result (each frame gravity takes its toll, so you'll want to aim higher than the target spring). Basics for jumping seem well described there: gamedev.stackexchange.com/a/29618/40264 \$\endgroup\$ – Vaillancourt Mar 11 '15 at 14:31
  • \$\begingroup\$ It worked, cheers. \$\endgroup\$ – eyes enberg Mar 11 '15 at 15:09
  • 1
    \$\begingroup\$ @eyesenberg If this answe worked for you, please give it an upvote and mark it as the accepted answer. This helps future comers to the site to identify the best answers, and if they worked or not. \$\endgroup\$ – Pip Aug 29 '15 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.