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

Set part.angle in Body.update. #298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kenrose
Copy link

@kenrose kenrose commented Oct 4, 2016

This change fixes a discrepency between Body.setAngle and Body.update.

Calling part.angle should always return the world space angle of the part.

In Body.setAngle, part.angle is correctly updated for every part. This maintains the invariant.

In Body.update, part.angle was previously not being updated. This led to stale values returned for part.angle.

Here's an example demonstrating that behaviour (paste into examples/):

(function() {

    var World = Matter.World,
        Bodies = Matter.Bodies,
        Body = Matter.Body,
        Common = Matter.Common,
        Events = Matter.Events;

    Example.angleSetting = function(demo) {
        var engine = demo.engine,
            world = engine.world;

        function inDegrees(radians) {
            return radians * (180.0 / Math.PI);
        }

        function inRadians(degrees) {
            return degrees * (Math.PI / 180.0);
        }

        function createBodyWithParts(x, y) {
            var width = 200;
            var height = 100;

            var outer = Bodies.rectangle(x, y, width, height);
            var inner = Bodies.rectangle(x, y + height / 2, width / 5, height / 5);
            Body.setAngle(inner, inRadians(30));
            return Body.create({parts: [outer, inner]});
        }

        function getInnerPart(body) {
            return body.parts[body.parts.length - 1];
        }

        var bodies = [
            createBodyWithParts(200, 200),
            createBodyWithParts(500, 200)
        ];

        // Use setAngle for one body
        Body.setAngle(bodies[0], inRadians(45));

        // Apply some angular acceleration on the other body
        Body.setAngularVelocity(bodies[1], 0.05);

        world.gravity = {x: 0.0, y: 0.0};
        World.add(world, bodies);

        var timeout = 5000; // ms
        window.setTimeout(function() {
            bodies.forEach(function(body) {
                var part = getInnerPart(body);
                console.log('body id ', body.id, ' has angle ', inDegrees(part.angle));
            });
        }, timeout);
    };
})();

This change fixes a discrepency between `Body.setAngle` and `Body.update`.

Calling `part.angle` should always return the world space angle of the part.

In `Body.setAngle`, `part.angle` is correctly updated for every part.
This maintains the invariant.

In `Body.update`, `part.angle` was previously not being updated.
This led to stale values returned for `part.angle`.
@kenrose
Copy link
Author

kenrose commented Mar 17, 2017

Any update on merging this PR?

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

Successfully merging this pull request may close these issues.

None yet

1 participant