Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a closed system? #144

Closed
kirtangajjar opened this issue Aug 27, 2015 · 2 comments
Closed

How to create a closed system? #144

kirtangajjar opened this issue Aug 27, 2015 · 2 comments

Comments

@kirtangajjar
Copy link

I wanted to create a system in which once the force to ball is applied, it keeps on moving forever without stopping, or reduction in speed.

I set restitution to 1,
set friction and frictionAir to 0 and
zeroed the gravity,

still the ball loses velocity over time. What am i missing?

Here's the code -

var Engine = Matter.Engine,
            World = Matter.World,
            Bodies = Matter.Bodies;

            // create a Matter.js engine
            var engine = Engine.create(document.body,{
                render:{
                    options:{
                        wireframes: true,
                        showAngleIndicator: true
                    }
                }
            });

            engine.world.gravity.x = 0;
            engine.world.gravity.y = 0;

            // create two boxes and a ground
            var boxA = Bodies.circle(400, 200, 80);
            var boxB = Bodies.rectangle(450, 50, 80, 80);
            var offset = 10;
            var ceiling = Bodies.rectangle(400, -offset, 800.5 + 2 * offset, 50.5, { isStatic: true });
            var ground = Bodies.rectangle(400, 600 + offset, 800.5 + 2 * offset, 50.5, { isStatic: true });
            var leftSide = Bodies.rectangle(800 + offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true });
            var rightSide = Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true });

            boxA.restitution = 1;
            boxA.friction = 0;
            boxA.frictionAir = 0;
            boxA.force = {x:1,y:1};

            // add all of the bodies to the world
            World.add(engine.world, [boxA, ceiling, ground, leftSide, rightSide]);

            // run the engine
            Engine.run(engine);
@liabru
Copy link
Owner

liabru commented Aug 27, 2015

You're losing linear velocity when it gets converted to angular velocity. Though, there is a problem because this doesn't look to be conserved very well at all (eventually the circle both spins slowly and travels slowly), so I should probably look at that.

In your case though, an easy solution is to set inertia to Infinity so that the body can't rotate. This seems to make it bounce at speed for far longer:

var boxA = Bodies.circle(400, 200, 80, { inertia: Infinity });

Even still though this may not last forever or it may fluctuate a bit, I'm not sure. If this is a problem or you need rotation, I think your best option is to just artificially maintain velocity by checking it every step and adding or subtracting the magnitude difference from your target velocity (so maintaining direction).

Also make sure you're using the latest master build.

@kirtangajjar
Copy link
Author

Thanks! setting Inertia to Infinity solved the problem. Actually i was planning to build pong in this, so i don't need rotation.

If you have any suggestion, please feel free to share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants