This repository has been archived by the owner on Dec 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Queries
Jasper edited this page Apr 22, 2014
·
2 revisions
Problem: Repeated searching through arrays to find specific objects.
Solution: Physics.query
Queries are test functions that can be run on an object to see if it matches a specific format. Query functions are created with the Physics.query helper which follows mongodb-like syntax.
Example:
var wheelsArray = [];
var queryFn = Physics.query({
name: 'circle', // only circles
$nin: wheelsArray, // not in the wheelsArray
labels: { $in: [ 'player', 'monster' ] } // that have player OR monster labels
});
var obj = {
name: 'circle',
labels: [ 'round' ]
};
queryFn( obj ); // -> false
// give it a player tag
obj.labels.push('player');
queryFn( obj ); // -> true
// put it inside the wheelsArray
wheelsArray.push( obj );
queryFn( obj ); // -> false
Queries can be used in conjunction with Physics.util.find and Physics.util.filter to test arrays.
You can also use the world.find and world.findOne methods to specifically retrieve bodies from the world using query searches.