-
Minimal programming experience, moderately proficient in Max, learning javascript to help write some more complex Max patches. No previous javascript experience and text-coding experience primarily in BASIC on a ZX 48k in 1986.
I want to write a javascript that takes inputs describing the position of two paddles, and outputs the position of a ball. I'm roughly getting there, but I can't work out how to get my function to continually output the ball position - it currently only outputs something when it gets a float input.
Can anyone help or point me towards good javascript tutorials that are relevant for max?
Thanks! Code below. Don't cringe.
d
autowatch = 1;
inlets = 2;
outlets = 2;
var pad1pos;
var pad2pos;
var pad1speed;
var pad2speed;
var ballpos = 0.0;
var ballspeed = 0.0;
function p(a,b,c,d) {
pad1pos=a;
pad2pos=b;
pad1speed=c;
pad2speed=d;
if (pad1pos==ballpos) {
ballspeed=pad1speed;
}
if (pad2pos=-ballpos) {
ballspeed=pad2speed;
}
ballpos=ballpos+ballspeed;
outlet(0, ballpos);
outlet(1, ballspeed);
} -
don't really know js but might be worth having a look at corners as it essential moves a ball around but with added gravity and friction maths. might help
-
you'll need to send / create an event system that triggers the calculation. inside of max javascript only runs in response to a command. at the moment your output will only be triggered when p() is called by the input.
you'll need to send the bang from a metro object in max, or alternatively create a timer inside js (but i don't know how to do that). -
there's a really good delicious max tutorial on youtube which shows you how to do a bouncing ball in max just using dicts. you could probably adapt the triggering mechanism from that to trigger the calc in your js object.
-
Those are great suggestions, thank you.
-
a couple of tehns recentish patches have nice little javascript bits that would be worth looking at.
-
Thanks @ootini - yes, I've been using sine-point.js a lot with the arc; it's one of the things that inspired me to get into it!
-
If the X and Y paddles haven't moved, and the ball's position hasn't changed, why are you looking to waste CPU cycles exactly?
-
Thanks @raja :) - I hadn't come across the jitter java folder before.
-
Interesting, I had no idea MAX/MSP supported JavaScript. Is this recent or has it always done?
-
@karaokaze thanks for the detailed reply that's very interesting