Famous / engine

MIT License
1.75k stars 249 forks source link

feat: Velocity System [Comments Wanted] #368

Open jd-carroll opened 9 years ago

jd-carroll commented 9 years ago

I am going to get started on a VelocitySystem, it will be the same as all of the existing systems where it updates every frame. Unlike the existing component/system frameworks, you must opt-in by doing something like:

function MyCustomNode() {
    Node.call(this);
    this._velocityID = this.addComponent(new Velocity());
    ...
}

(Meaning if you init the node, the Velocity component will not come by default.)

There are several advantages to this approach, and it should generate very smooth velocity components. I'm thinking of using the PhysicsEngine and I might try to coerce it into generating the Velocities, but I'm open to other suggestions.

Then you would add logic something along the lines of:

MyCustomNode.prototype.onReceive = function onReceive(type, event) {
    this.getVelocity().addEvent(type, event);
    ...
}

The VelocitySystem would take care of direction changes, pauses, increasing / decreasing speed, etc.

Please share any comments / questions / concerns.

jd-carroll commented 9 years ago

So, updating the VelocitySystem every frame would not work.. There would have to be corresponding touch/mouse events for every frame, which will not be the case.

Thus, it will be better to build this as a standalone piece and not as one of the core Systems.

alexanderGugel commented 9 years ago

I'm not really sure what the use case for such a system would be/ why it's needed. Can you give an example and propose an API?

jd-carroll commented 9 years ago

@alexanderGugel The purpose of such a system is pretty obvious, calculate velocity of a touchmove for use in the PhysicsEngine. My original thought of creating some type of system will not work, but having something that calculates the delta/velocity from touch events is very important.

alexanderGugel commented 9 years ago

The purpose of such a system is pretty obvious, calculate velocity of a touchmove for use in the PhysicsEngine.

I don't think we really need a system for that. While I agree that knowing the velocity of a touchmove is useful in a lot of cases, I don't think it makes sense to architect this as a separate system. I think adding custom components solves this use case:

node.addComponent({
    onReceive: function(type, ev) {
        switch (type) {
            case 'mousemove':
            case 'touchmove':
                // Calculate velocity here
                break;
        }
    }
});
michaelobriena commented 9 years ago

@jd-carroll do you have issues with @alexanderGugel's proposed solution?

jd-carroll commented 9 years ago

Didn't realize I never responded.. That's actually what I had implemented: https://github.com/jd-carroll/engine-seed/blob/scrollViewSample/src/index.js#L312

But, I think this subject needs to be revisited. Whether touchmove or mousemove we need something that can calculate the delta. The little sample I have only works for touchmove and (since it was a copy from the old framework) the manner in which velocity is calculated isn't great.