mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
rebuilt skateboard without joints
This commit is contained in:
parent
97da7770d0
commit
da62339a95
2 changed files with 118 additions and 19 deletions
|
|
@ -12,16 +12,114 @@ function (Parent, Box2D, Settings, Assert) {
|
||||||
function Skateboard(physicsEngine, uid, options) {
|
function Skateboard(physicsEngine, uid, options) {
|
||||||
|
|
||||||
Parent.call(this, physicsEngine, uid, options);
|
Parent.call(this, physicsEngine, uid, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
Skateboard.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
|
Skateboard.prototype.createFixture = function () {
|
||||||
|
Assert.number(this.options.width, this.options.height);
|
||||||
|
Assert.number(this.options.weight);
|
||||||
|
|
||||||
|
var deckShape = new Box2D.Collision.Shapes.b2PolygonShape();
|
||||||
|
var w = this.options.width / Settings.RATIO;
|
||||||
|
var h = 2 / Settings.RATIO;
|
||||||
|
deckShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, -(4.5 / Settings.RATIO)));
|
||||||
|
|
||||||
|
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
||||||
|
fixtureDef.shape = deckShape;
|
||||||
|
|
||||||
|
var offset = 4,
|
||||||
|
factor = 80;
|
||||||
|
var density = ((this.options.weight + offset) / this.options.width / this.options.height) * factor;
|
||||||
|
fixtureDef.density = density;
|
||||||
|
fixtureDef.friction = Settings.ITEM_FRICTION;
|
||||||
|
fixtureDef.restitution = 0.2;
|
||||||
|
fixtureDef.isSensor = false;
|
||||||
|
|
||||||
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
|
|
||||||
|
this.addWheel(
|
||||||
|
-8,
|
||||||
|
-2.5
|
||||||
|
);
|
||||||
|
|
||||||
|
this.addWheel(
|
||||||
|
7,
|
||||||
|
-2.5
|
||||||
|
);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Skateboard.prototype.addWheel = function(x, y) {
|
||||||
|
Assert.number(x, y);
|
||||||
|
|
||||||
|
var bodyDef = new Box2D.Dynamics.b2BodyDef();
|
||||||
|
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||||
|
bodyDef.position.x = x / Settings.RATIO;
|
||||||
|
bodyDef.position.y = y / Settings.RATIO;
|
||||||
|
bodyDef.angle = 0;
|
||||||
|
|
||||||
|
var wheelShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||||
|
wheelShape.SetRadius(2.5 / Settings.RATIO);
|
||||||
|
wheelShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
|
||||||
|
|
||||||
|
|
||||||
|
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
||||||
|
var offset = 4,
|
||||||
|
factor = 80;
|
||||||
|
var density = ((0.1 + offset) / 3 / 3) * factor;
|
||||||
|
fixtureDef.density = density;
|
||||||
|
fixtureDef.shape = wheelShape;
|
||||||
|
fixtureDef.restitution = 0.2;
|
||||||
|
fixtureDef.isSensor = false;
|
||||||
|
fixtureDef.friction = 0.0005;
|
||||||
|
|
||||||
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Skateboard.prototype.flip = function(direction) {
|
||||||
|
this.flipDirection = direction;
|
||||||
|
|
||||||
|
// FIXME: implement body flip if necessary
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return Skateboard;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
define([
|
||||||
|
"Game/" + GLOBALS.context + "/GameObjects/Item",
|
||||||
|
"Lib/Vendor/Box2D",
|
||||||
|
"Game/Config/Settings",
|
||||||
|
"Lib/Utilities/Assert"
|
||||||
|
],
|
||||||
|
|
||||||
|
function (Parent, Box2D, Settings, Assert) {
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
function Skateboard(physicsEngine, uid, options) {
|
||||||
|
|
||||||
|
Parent.call(this, physicsEngine, uid, options);
|
||||||
|
|
||||||
this.wheels = [
|
this.wheels = [
|
||||||
this.addWheel(
|
this.addWheel(
|
||||||
options.x + 8,
|
options.x + 8,
|
||||||
options.y - 1.5
|
options.y - 1.5
|
||||||
),
|
),
|
||||||
this.addWheel(
|
this.addWheel(
|
||||||
options.x - 8,
|
options.x - 8,
|
||||||
options.y - 1.5
|
options.y - 1.5
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,13 +151,13 @@ function (Parent, Box2D, Settings, Assert) {
|
||||||
Skateboard.prototype.addWheel = function(x, y) {
|
Skateboard.prototype.addWheel = function(x, y) {
|
||||||
Assert.number(x, y);
|
Assert.number(x, y);
|
||||||
|
|
||||||
var bodyDef = new Box2D.Dynamics.b2BodyDef();
|
var bodyDef = new Box2D.Dynamics.b2BodyDef();
|
||||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||||
bodyDef.position.x = x / Settings.RATIO;
|
bodyDef.position.x = x / Settings.RATIO;
|
||||||
bodyDef.position.y = y / Settings.RATIO;
|
bodyDef.position.y = y / Settings.RATIO;
|
||||||
bodyDef.angle = 0;
|
bodyDef.angle = 0;
|
||||||
|
|
||||||
var wheelShape = new Box2D.Collision.Shapes.b2CircleShape();
|
var wheelShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||||
wheelShape.SetRadius(1.5 / Settings.RATIO);
|
wheelShape.SetRadius(1.5 / Settings.RATIO);
|
||||||
wheelShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, 0));
|
wheelShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, 0));
|
||||||
|
|
||||||
|
|
@ -75,18 +173,18 @@ function (Parent, Box2D, Settings, Assert) {
|
||||||
var wheelBody = this.body.GetWorld().CreateBody(bodyDef);
|
var wheelBody = this.body.GetWorld().CreateBody(bodyDef);
|
||||||
wheelBody.CreateFixture(fixtureDef);
|
wheelBody.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
//var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
|
//var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
|
||||||
var revoluteJointDef = new Box2D.Dynamics.Joints.b2WeldJointDef();
|
var revoluteJointDef = new Box2D.Dynamics.Joints.b2WeldJointDef();
|
||||||
//revoluteJointDef.enableMotor = false;
|
//revoluteJointDef.enableMotor = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.GetWorldCenter());
|
revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.GetWorldCenter());
|
||||||
var j = this.body.GetWorld().CreateJoint(revoluteJointDef);
|
var j = this.body.GetWorld().CreateJoint(revoluteJointDef);
|
||||||
|
|
||||||
|
|
||||||
// FIXME this means, that we will have bodies in the world, which must not be
|
// FIXME this means, that we will have bodies in the world, which must not be
|
||||||
// updated (wheels) because they are always connected to a body which will be updated.
|
// updated (wheels) because they are always connected to a body which will be updated.
|
||||||
return wheelBody;
|
return wheelBody;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -116,4 +214,5 @@ function (Parent, Box2D, Settings, Assert) {
|
||||||
|
|
||||||
return Skateboard;
|
return Skateboard;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
@ -46,8 +46,8 @@ define([
|
||||||
Level.prototype.createItem = function(uid, options) {
|
Level.prototype.createItem = function(uid, options) {
|
||||||
|
|
||||||
switch(options.type) {
|
switch(options.type) {
|
||||||
//case "skateboard":
|
case "skateboard":
|
||||||
// return new Skateboard(this.engine, uid, options);
|
return new Skateboard(this.engine, uid, options);
|
||||||
case "ragdoll":
|
case "ragdoll":
|
||||||
return new RagDoll(this.engine, uid, options);
|
return new RagDoll(this.engine, uid, options);
|
||||||
case "rube":
|
case "rube":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue