piqnt / planck.js

2D JavaScript Physics Engine
http://piqnt.com/planck.js/
MIT License
4.9k stars 236 forks source link

Rebuilding planck.js used by testbed #1

Closed 1pakch closed 7 years ago

1pakch commented 7 years ago

I am porting the Top-down car demo and computing a projection of one vector on another one is quite a common operation there. Hence I implemented a projectOn method for the Vec2 class and the issue is that I am unable to run a testbed so that it picks up the new version of the Vec2 class.

While I am able to rebuild dist/planck.js using gulp so that it includes the new method it is still not picked up by the testbed. Naturally, I am wondering what goes wrong.

I am new to js so I apologize if the issue is trivial.

shakiba commented 7 years ago

Testbed does not use dist/planck.js, it serves and uses a live build. You could check in the browser to see if it is added.

By the way, isn't it possible to use dot function for projection?

1pakch commented 7 years ago

I even see this new method in Developer Tools sources of Vec2 in Chromium. Regardless of this it is undefined on Vec2 objects spit out by the library. I am a complete JS newbie, so maybe my comprehension of what is going on is a bit off.

I meant projection as a vector, i.e.

project = function(v, w) {
    Vec2.assert(v);
    Vec2.assert(w);
    return w.mul(Vec2.dot(v, w));
}

IMO something like v.projectOn(w) reads better as the geometric meaning is immediately clear.

shakiba commented 7 years ago

Can you please include the exact code that you are adding?

You could either do Vec2.projectOn = function(v, w){...} or Vec2.prootype.projectOn = function(w){...} which are a little different. (I recommend studying prototypes in JS if you are new to this topic.)

1pakch commented 7 years ago

It is the exact code except for the name. The issue was that I did not understand the different ways of defining methods in JS. As I see it now

Many thanks, you pointed me in the right direction. The only thing I am not quite sure about is the call Vec2.prototype.projectOn(x, y). Why and how it works so that x is passed as an Object and not as Vec2? (I guess I should read a bit on JS type system as it is a bit different from what I am used to (Python, C++)).