planck.js
planck.js copied to clipboard
What is the origin of a body with fixtures?
I'm trying to create a logo using planck.js. The idea is to create a SVG based on the state of the physics.
I'm pretty far but I can't figure out what is going on with the positions.
Those boxes:
Should match in some way this:
It only looks correct at the animation start.
I made a codepen https://codepen.io/clankill3r/pen/bXvvaP
Hope you can have a look.
I might figured it out, here is the relevant code:
vecs_A_0 = rescale_offset_and_make_vec2(vecs_A_0, scale, -97, 377 - 88);
vecs_A_1 = rescale_offset_and_make_vec2(vecs_A_1, scale, -97, 377 - 88);
vecs_A_2 = rescale_offset_and_make_vec2(vecs_A_2, scale, -97, 377 - 88);
vecs_P_0 = rescale_offset_and_make_vec2(vecs_P_0, scale, -214, 377 - 88);
vecs_P_1 = rescale_offset_and_make_vec2(vecs_P_1, scale, -214, 377 - 88);
vecs_R_0 = rescale_offset_and_make_vec2(vecs_R_0, scale, -328, 377 - 88);
vecs_R_1 = rescale_offset_and_make_vec2(vecs_R_1, scale, -328, 377 - 88);
vecs_R_2 = rescale_offset_and_make_vec2(vecs_R_2, scale, -328, 377 - 88);
vecs_I_0 = rescale_offset_and_make_vec2(vecs_I_0, scale, -410, 377 - 88);
function create_body(name, vecs_arrays, pos) {
var body = world.createBody({
position : pos,
type : 'dynamic',
allowSleep : false
});
body.name = name;
for (var i = 0; i < vecs_arrays.length; i++) {
var vecs = vecs_arrays[i];
body.createFixture(pl.Polygon(vecs), 2.0); // what is 2.0?
}
return body;
}
body_A_0 = create_body("A_0", [vecs_A_0, vecs_A_1, vecs_A_2], Vec2(-r + 0.1633 * (r+r), 2*88*scale));
body_P = create_body("P", [vecs_P_0, vecs_P_1], Vec2(-r + 0.3603 * (r+r), 2*88*scale));
body_R = create_body("R", [vecs_R_0, vecs_R_1, vecs_R_2], Vec2(-r + 0.5526 * (r+r), 2*88*scale));
body_I = create_body("I", [vecs_I_0], Vec2(-r + 0.6904 * (r+r), 2*88*scale));
body_A_1 = create_body("A_1", [vecs_A_0, vecs_A_1, vecs_A_2], Vec2(-r + 0.8407 * (r+r), 2*88*scale));
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
testbed.step = function() {
update_apria_svg_logos();
};
function dbg_test(body, color, width, height) {
var x = map(body.getPosition().x, -15, 15, 0, 100);
var y = map(body.getPosition().y-2*88*scale, -15, 15, 100, 0);
var a = -body.getAngle() * R2D;
var hx = width/2;
var hy = height/2;
d3.selectAll(".apria_logo")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr("style", "fill:"+color)
.attr("transform", "translate("+x+", "+y+") rotate("+a+", "+hx+", "+hy+")")
;
}
So I basically changed the offset parameter to 377 - 88
, so the shapes are centered around the origin, and changed hx
and hy
, so that the rect is rotated around its center.
Let me know if it works.
BTW: You duplicated the coordinates in your vecs array. I think
vecs_A_0 = [116, 201, 148, 377, 119, 377, 113, 338, 116, 201];
[...]
should also work.
Is the problem solved? By the way, this is so cool! :)
I just published a small change to make it possible to change testebed y-axis direction and match it with svg coordinate. From v0.3.9 you can set testbed.scaleY *= -1
and it will reverse y direction. Here is same code (with zOadT's changes) and reversed y-axis (please note gravity is positive now):
https://codepen.io/piqnt/pen/GVYMLK
Sorry for the late reply, I was on vacation.
@zOadT @shakiba
The problem is still there, It is better but after a while when things rotated more you can see the issues:
![Screen Shot 2019-08-29 at 18 58 21](https://user-images.githubusercontent.com/738650/63960865-bd811c00-ca8f-11e9-929b-61eeebab68ed.png)
![Screen Shot 2019-08-29 at 18 58 19](https://user-images.githubusercontent.com/738650/63960871-c1ad3980-ca8f-11e9-8c99-3c37ff676fed.png)
I made a new codepen (using version 0.39) where I remade a lot of things. I wanted to understand the problem and not eyeball a fix.
One thing I did was wrong was the point of rotation. I am nearly there, the only thing wrong is that the position is a bit off.
https://codepen.io/clankill3r/pen/jONwvdY
Alright, the big wheel is not centered, but it's rotating off center. Place your cursor at the top of the wheel for example and keep watching. The fix should be really simple but I don't know how.
![Screen Shot 2019-08-29 at 19 40 06](https://user-images.githubusercontent.com/738650/63963257-eeb01b00-ca94-11e9-8e23-dde766954625.png)
![Screen Shot 2019-08-29 at 19 39 00](https://user-images.githubusercontent.com/738650/63963267-f374cf00-ca94-11e9-9eaa-7db9adf6d4e0.png)
Cool! Your hint was actually very helpful, the problem was reusing y
variable, here is the fix.
~I randomized boxes' location, probably you want to remove that!~