I've started HTML 5 / Canvas programming, and I've got to say it's a lot of fun. One thing I'll never get over is the lack of a built-in function for collision detection. I miss "touching?" so much!
Does anyone have any tips for JavaScript collision detection? I don't want to use bounding boxes (i.e. rectangular collision), and short of making a list of all the pixels in an image and comparing it to another, I'm out of ideas. Scratch on!
EDIT: On a completely unrelated note, why is it common for JavaScript programmers to set a variable to a function and call the variable?
For example, they do this:
var example = function() { some code goes here } example();
Instead of this:
function example () { some code goes here } example();
Last edited by shadow_7283 (2012-06-13 18:34:55)
Offline
Many blocks in Scratch provide functions that tend to be done manually by the programmer, in the form of a function, in pretty much every other language. Considering this, you could take a look at Scratch's source code in Squeak, yoy may learn something from it.
I tried a quick Google search but pretty much everything I can find involves either bounding box collision, or a pixel map as you mentioned. Why exactly can't you do the latter?
Offline
shadow_7283 wrote:
On a completely unrelated note, why is it common for JavaScript programmers to set a variable to a function and call the variable?
For example, they do this:Code:
var example = function() { some code goes here } example();Instead of this:
Code:
function example () { some code goes here } example();
I think I may know why, but I'm just taking a guess here.
I think that they are trying to move more towards OOP and away from other types of programming.
Offline
technoguyx wrote:
I tried a quick Google search but pretty much everything I can find involves either bounding box collision, or a pixel map as you mentioned. Why exactly can't you do the latter?
It's not so much a requirement as a preference. The collision needs to be calculated fairly quickly with little resource expense, and sorting through thousands of pixels every cycle isn't what I'd call efficiency. But hopefully JavaScript can pull it off, and in any event I'm looking to use the Box2D JavaScipt port for collision and physics.
Offline
SJRCS_011 wrote:
I think that they are trying to move more towards OOP and away from other types of programming.
The strange thing is, to the interpreter that doesn't really make a difference- it means and does the same thing. The only thing that I thought of in favor of the OOP version is that it might be a global/local variable thing.
If you had this piece of code:
function example () { do something function nested () { do something else } }
Could you call nested() from outside the example() function?
I know you can in the following example.
function example () { do something nested = function () { do something else } }
(I'll try it out tomorrow, right now I'm typing on my iPod and my brain is fried. )
Offline
shadow_7283 wrote:
If you had this piece of code:
Code:
function example () { do something function nested () { do something else } }Could you call nested() from outside the example() function?
Nope! These two different ways to declare a function are basically identical, except for one small difference.
myFunc(); function myFunc() { console.log("Hello, world!"); }
This is perfectly valid code, but this is not:
myFunc(); var myFunc = function() { console.log("Hello, world!"); }
In the second example myFunc is called before it is defined. But if you declare a function the first way, the interpreter binds it immediately as if it were at the top of the script or the function that contains it. So why write functions the second way? For fun, I guess. Personally I use the first way for global functions and the second way for nested functions, 'cuz I like the way it looks.
Last edited by fullmoon (2012-06-14 01:46:34)
Offline
I think there was a post somewhere around here saying how collision works in Scratch and that it uses bounding box, then if it reports true, it does something else to check if they are actually touching.
Offline
Thanks for clearing that up fullmoon! I'm not done with this thread though; you all are going to have to help this novice every step of the way! Does anyone have experience with Box2D that they would be willing to share? The problem with JavaScript Box2D is that it is basically a direct translation of the AS3 version, so it has no documentation whatsoever. In particular, I'm not quite sure how to define specific verticies in a polygon.
Offline
If anyone is interested in how the scratch function works, take a look through this thread
Offline
*bump*
Offline
shadow_7283 wrote:
Does anyone have experience with Box2D that they would be willing to share? The problem with JavaScript Box2D is that it is basically a direct translation of the AS3 version, so it has no documentation whatsoever. In particular, I'm not quite sure how to define specific verticies in a polygon.
I don't know much about Box2D at all, but have you seen this? The JS and AS should be pretty much identical.
Offline
Thanks again, fullmoon. :3
Offline