Giter Club home page Giter Club logo

Comments (4)

shakiba avatar shakiba commented on May 11, 2024

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?

from planck.js.

1pakch avatar 1pakch commented on May 11, 2024

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.

from planck.js.

shakiba avatar shakiba commented on May 11, 2024

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.)

from planck.js.

1pakch avatar 1pakch commented on May 11, 2024

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

  • Vec2.projectOn = function(v, w){...} defines a static method which can only be invoked via Vec2.projectOn(x,y) but not x.projectOn(y).
  • Vec2.prototype.projectOn = function(other){...} defines a method for all instances that can be invoked via x.projectOn(y) (and Vec2.prototype.projectOn(x, y) but in this case x will be an Object and not a Vec2)
  • One can also define an instance method directly in an object constructor. Unlike the prototype version the implementation code object will not be shared across instances. The benefit is that one can use constructor parameters explicitly which is clearly not possible with prototype methods (one needs pass everything via this).

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++)).

from planck.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.