collision detection

269 views
Skip to first unread message

teachertinker

unread,
Feb 16, 2017, 4:37:35 PM2/16/17
to CoSpaces Scripting
Hello --  I am trying to create a way to detect if two things collide using Blockly.  For example, if a ball moves and eventually touches a person, for there to be some kind of response.  How can I do collision detection using Blockly code?

Benjamin Singh

unread,
Feb 17, 2017, 3:29:42 AM2/17/17
to CoSpaces Scripting
Hi,

for simple collision detection we can check the distance between 2 objects:


However, in order to make this check over and over again, which is what we need to do, we need an update loop (a block like "repeat"). Such a block is not available at the moment but will be introduced with a future update. So at the moment it's just possible using JavaScript. Do you use JavaScript as well?


Benjamin


teachertinker

unread,
Feb 17, 2017, 9:52:51 AM2/17/17
to CoSpaces Scripting
Yes. Can you please show me sample javascript code for this?

Collision detection is very important for making most games (bullet hitting a spaceship, mario touching a coin, etc.) so I hope this feature will come to Blockly soon.  

Daniel Pers

unread,
Feb 19, 2017, 11:03:45 AM2/19/17
to CoSpaces Scripting
Hi teachertinker,

I also tried to simulate the detection of a risk of collision between different fixed or moving objects.
Here are some of my tests with Blockly :



Daniel Pers

unread,
Feb 19, 2017, 12:39:07 PM2/19/17
to CoSpaces Scripting

teachertinker

unread,
Feb 19, 2017, 12:51:44 PM2/19/17
to CoSpaces Scripting
Very cool. thanks Daniel for your example.  It seems very time consuming to write the code detect a collision.  I really hope this becomes simplified in Blockly -- as collisions are very fundamental to games.  I guess there are "bounding box" issues that are tricky.  If Blockly is too tricky right now I'm wondering how to implement collisions in Javascript.

Daniel Pers

unread,
Feb 19, 2017, 1:22:57 PM2/19/17
to CoSpaces Scripting
Hi,

I dontt really agree with you.
With some practice, you can write this kind of program quite quickly with Blockly. The program I propose with Bockly is quite close to the algorithm of a real solution implemented with an Arduino card for example. It is understandable by a student who has some basics, from 13 years old I think.
If we simplify too much, students will no longer see the treatment that a real system must perform to manage a situation that is very simple.
If we want to use this type of solution with Blockly in more complex situations, it would be necessary to define functions such as : Identify the nearest obstacle, Identify the direction of the obstacle to be avoided, Perform an avoidance maneuver, ... Which would simplify the main program. Benjamin said that functions would soon be available in Blockly.

teachertinker

unread,
Feb 19, 2017, 1:36:37 PM2/19/17
to CoSpaces Scripting
Well, in Actionscript, for example, the built-in hitTestObject() and hitTestPoint() methods made life very easy and allowed coders to focus on other things rather than building our own collision detection system.  

Daniel Pers

unread,
Feb 20, 2017, 4:53:40 AM2/20/17
to CoSpaces Scripting
Hi,
You can do this also by creating functions in Blockly. This work is interesting from an educational point of view. Students can also be given the functions if they don't wish to enter this part. Actionscript is obsolete, while Blockly is now the standard used by most of new learning software programs.

Benjamin Singh

unread,
Feb 20, 2017, 5:32:03 AM2/20/17
to cospaces-...@googlegroups.com
Hi all!

To make a simple collision detection in JavaScript we can use the item.distanceToItem(otherItem) method like this:

// create person and ball
var person = Scene.createItem('LP_Wom', 0, 0, 0);
var ball = Scene.createItem('Sphere', 0, 5, 0);

// move ball towards person
ball.move(0, -10);

// check distance repeatedly
Scene.scheduleRepeating(function() {
    if(person.distanceToItem(ball) < 0.5) {
        person.say('Collision!');
    } else {
        person.say('');
    }
}, 0);


This may work in most simple cases but it has its limitations. This is why we're working on a collision detection API which will be available in the future. With the help of these methods it will be possible to detect collisions between complex objects without the need of an update loop. We can add an event listener instead. We're also discussing how to make these methods available in Blockly as well.

It's also possible to write your own collision detection system using JavaScript or Blockly, but this can be a challenging task in 3D if the scene is dynamic and the objects are complex.

Thanks,
Benjamin

Daniel Pers

unread,
Feb 20, 2017, 10:20:56 AM2/20/17
to CoSpaces Scripting
Hi Benjamin,

Thank you for your help.
Here is a first version of the tests I currently do on the possibilities of interacting with a system programmed with Blockly :
Looks like we had the same idea, but I work more on the interactivity than on the detection of collisions.

Il also wrote this message on Facebook (CoSpaces classroom) :

Benjamin Singh

unread,
Feb 20, 2017, 11:37:45 AM2/20/17
to cospaces-...@googlegroups.com
Hi Daniel,

very cool! Interesting way to create an update loop, haven't thought of doing it this way :)

Yes we saw your message on Facebook and some things are already planned, e.g. blocks for functions and playing sound. We will have look at all the other things you requested.

Thanks,
Benjamin
Reply all
Reply to author
Forward
0 new messages