Jozef
Jozef

Reputation: 523

Physicsjs Screen wrap

I am currently having a bit of trouble making objects in my world wrap. It sort of works, but very often objets appear to get stuck on the boundaries. My wrap code is as follows:

        // Wrap our position if we are outside of the world bounds
        if (this.state.pos.get(0) > 860) {
            this.state.pos.set(0, this.state.pos.get(1));
        }
        else if (this.state.pos.get(0) < 0) {
          this.state.pos.set(860, this.state.pos.get(1));
        }

        if (this.state.pos.get(1) > 640) {
          this.state.pos.set(this.state.pos.get(0), 0);
        }
        else if (this.state.pos.get(1) < 0) {
          this.state.pos.set(this.state.pos.get(0), 640);
        }

Is there a better way of doing this? Should I use a translation on the object's position vector rather than simply setting it?

Upvotes: 2

Views: 83

Answers (1)

Jasper
Jasper

Reputation: 1193

Without a jsfiddle it's a bit hard to diagnose, however this might be due to the this.state.old.pos not being set too. If the position (only) is set, then the velocity will be calculated as the difference between the current and the previous positions (in accordance with verlet integration). In that case, you're implicitly giving the body a huge velocity.

I'd recommend adding/subtracting an amount rather than setting, then you can do the same with the old position.

Here's a working example: http://labs.minutelabs.io/Relativistic-Space-Sheep/ With the relevant line of code: https://github.com/minutelabsio/Relativistic-Space-Sheep/blob/master/library/js/mediators/boilerplate.js#L743

Upvotes: 2

Related Questions