<commit_before>/*global Box2D:true */

'use strict';

/**
 * The `rigidbody2d` component provides a wrapper for methods in the Box2D API.
 * 
 * @author <NAME>
 */
Javelin.Component.Rigidbody2d = function(gameObject, component) {
    var box2d = gameObject.engine.getPlugin('box2d');
    //the gameObject's transform
    var transform = gameObject.getComponent('transform2d');
    var debug = gameObject.engine.debug;
    
    //box2d stuff used internally
    var bodyDef = null;
    var fixtureDef = null;
    var body = null;
    var fixture = null;
    
    
    //these values are applied to the box2d fixture definition
    component.trigger = false;
    component.static = false;
    component.bullet = false;
    component.density = 1.0;
    component.friction = 0.0;
    component.restitution = 0.0;
    component.damping = 0.2;
    component.angularDamping = 0.3;
    component.fixedRotation = false;
    component.radius = null;
    component.shape = null; //array of custom shape points
    component.height = null;
    component.width = null;
    
    component.applyForce = function(degrees, power) {
        body.ApplyImpulse(new box2d.Vec2(Math.cos(degrees * Javelin.PI_OVER_180) * power, Math.sin(degrees * Javelin.PI_OVER_180) * power), body.GetWorldCenter());
    };
    component.applyForceForward = function(amount) {
        component.applyForce(transform.rotation, amount);
    };
    component.applyForceBackward = function(amount) {
        component.applyForce((transform.rotation + 180 % 360), amount);
    };
    component.applyForceLeft = function(amount) {
        component.applyForce((transform.rotation - 90 % 360), amount);
    };
    component.applyForceRight = function(amount) {
        component.applyForce((transform.rotation + 90 % 360), amount);
    };

    component.applyImpulse = function(degrees, power) {
        body.ApplyImpulse(new box2d.Vec2(Math.cos(degrees * Javelin.PI_OVER_180) * power, Math.sin(degrees * Javelin.PI_OVER_180) * power), body.GetWorldCenter());
    };
    component.applyImpulseForward = function(amount) {
        component.applyImpulse(transform.rotation, amount);
    };
    component.applyImpulseBackward = function(amount) {
        component.applyImpulse((transform.rotation + 180 % 360), amount);
    };
    component.applyImpulseLeft = function(amount) {
        component.applyImpulse((transform.rotation - 90 % 360), amount);
    };
    component.applyImpulseRight = function(amount) {
        component.applyImpulse((transform.rotation + 90 % 360), amount);
    };
    
    //TODO
    component.applyRotationForce = function(force) {
        body.ApplyTorque(force);
    };
    
    component.setVelocity = function(x, y) {
        body.SetLinearVelocity(new box2d.Vec2(x, y));
    };
    
    component.getVelocity = function() {
        return body.GetLinearVelocity();
    };

    //TODO: fill in API
    component.applyRotationImpulse = function(force, x, y) {};
    component.setAngularVelocity = function() {};
    component.getAngularVelocity = function() {};
    component.getInertia = function() {};
    component.teleport = function() {};
    
    component.reset = function() {
        //take into account potentially modified
        //component values - meaning change the fixture/body
        //reset mass data etc...
    };
    
    component.createBodyDefinition = function() {
        //create body definition
        bodyDef = new box2d.BodyDef();
        bodyDef.type = (component.static) ? box2d.Body.b2_staticBody : box2d.Body.b2_dynamicBody;
        bodyDef.position.x = transform.position.x / 100;
        bodyDef.position.y = transform.position.y / 100;
        bodyDef.angle = transform.rotation;
        bodyDef.linearDamping = component.damping;
        bodyDef.angularDamping = component.angularDamping;
        bodyDef.fixedRotation = component.fixedRotation;
        bodyDef.bullet = component.bullet;
        bodyDef.userData = gameObject;
        
        return bodyDef;
    };
    
    component.createFixtureDefinition = function() {
        //create fixture definition
        fixtureDef = new box2d.FixtureDef();
        fixtureDef.density = component.density;
        fixtureDef.restitution = component.restitution;
        fixtureDef.friction = component.friction;
        if (component.trigger) {
            fixtureDef.isSensor = true;
        }
//        fixtureDef.mass = component.mass;

        //set the fixture's shape - oooh boy.
        fixtureDef.shape = component.createFixtureShape();
        
        return fixtureDef;
    };
    
    //this mostly taken from gritsgame source - thanks!
    component.createFixtureShape = function() {
        //TODO: take into account scaling
        var shape;
        
        if (component.radius) {
            shape = new box2d.CircleShape(component.radius);<commit_msg>properly returing circle shape in rigidbody2d<commit_after>/*global Box2D:true */

'use strict';

/**
 * The `rigidbody2d` component provides a wrapper for methods in the Box2D API.
 * 
 * @author <NAME>
 */
Javelin.Component.Rigidbody2d = function(gameObject, component) {
    var box2d = gameObject.engine.getPlugin('box2d');
    //the gameObject's transform
    var transform = gameObject.getComponent('transform2d');
    var debug = gameObject.engine.debug;
    
    //box2d stuff used internally
    var bodyDef = null;
    var fixtureDef = null;
    var body = null;
    var fixture = null;
    
    
    //these values are applied to the box2d fixture definition
    component.trigger = false;
    component.static = false;
    component.bullet = false;
    component.density = 1.0;
    component.friction = 0.0;
    component.restitution = 0.0;
    component.damping = 0.2;
    component.angularDamping = 0.3;
    component.fixedRotation = false;
    component.radius = null;
    component.shape = null; //array of custom shape points
    component.height = null;
    component.width = null;
    
    component.applyForce = function(degrees, power) {
        body.ApplyImpulse(new box2d.Vec2(Math.cos(degrees * Javelin.PI_OVER_180) * power, Math.sin(degrees * Javelin.PI_OVER_180) * power), body.GetWorldCenter());
    };
    component.applyForceForward = function(amount) {
        component.applyForce(transform.rotation, amount);
    };
    component.applyForceBackward = function(amount) {
        component.applyForce((transform.rotation + 180 % 360), amount);
    };
    component.applyForceLeft = function(amount) {
        component.applyForce((transform.rotation - 90 % 360), amount);
    };
    component.applyForceRight = function(amount) {
        component.applyForce((transform.rotation + 90 % 360), amount);
    };

    component.applyImpulse = function(degrees, power) {
        body.ApplyImpulse(new box2d.Vec2(Math.cos(degrees * Javelin.PI_OVER_180) * power, Math.sin(degrees * Javelin.PI_OVER_180) * power), body.GetWorldCenter());
    };
    component.applyImpulseForward = function(amount) {
        component.applyImpulse(transform.rotation, amount);
    };
    component.applyImpulseBackward = function(amount) {
        component.applyImpulse((transform.rotation + 180 % 360), amount);
    };
    component.applyImpulseLeft = function(amount) {
        component.applyImpulse((transform.rotation - 90 % 360), amount);
    };
    component.applyImpulseRight = function(amount) {
        component.applyImpulse((transform.rotation + 90 % 360), amount);
    };
    
    //TODO
    component.applyRotationForce = function(force) {
        body.ApplyTorque(force);
    };
    
    component.setVelocity = function(x, y) {
        body.SetLinearVelocity(new box2d.Vec2(x, y));
    };
    
    component.getVelocity = function() {
        return body.GetLinearVelocity();
    };

    //TODO: fill in API
    component.applyRotationImpulse = function(force, x, y) {};
    component.setAngularVelocity = function() {};
    component.getAngularVelocity = function() {};
    component.getInertia = function() {};
    component.teleport = function() {};
    
    component.reset = function() {
        //take into account potentially modified
        //component values - meaning change the fixture/body
        //reset mass data etc...
    };
    
    component.createBodyDefinition = function() {
        //create body definition
        bodyDef = new box2d.BodyDef();
        bodyDef.type = (component.static) ? box2d.Body.b2_staticBody : box2d.Body.b2_dynamicBody;
        bodyDef.position.x = transform.position.x / 100;
        bodyDef.position.y = transform.position.y / 100;
        bodyDef.angle = transform.rotation;
        bodyDef.linearDamping = component.damping;
        bodyDef.angularDamping = component.angularDamping;
        bodyDef.fixedRotation = component.fixedRotation;
        bodyDef.bullet = component.bullet;
        bodyDef.userData = gameObject;
        
        return bodyDef;
    };
    
    component.createFixtureDefinition = function() {
        //create fixture definition
        fixtureDef = new box2d.FixtureDef();
        fixtureDef.density = component.density;
        fixtureDef.restitution = component.restitution;
        fixtureDef.friction = component.friction;
        if (component.trigger) {
            fixtureDef.isSensor = true;
        }
//        fixtureDef.mass = component.mass;

        //set the fixture's shape - oooh boy.
        fixtureDef.shape = component.createFixtureShape();
        
        return fixtureDef;
    };
    
    //this mostly taken from gritsgame source - thanks!
    component.createFixtureShape = function() {
        //TODO: take into account scaling
        var shape;
        
        if (component.radius) {
            shape = new box2d.CircleShape(component.radius);
            return shape;
{'fraction_non_alphanumeric': 0.1111225117997127, 'fraction_numerical': 0.016930022573363433, 'mean_word_length': 2.7259174311926606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731560', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2771, 'n_words': 576}