By default all objects have physics turned on. This may have been noticeable if you tried to change the position of one sprite and it overlaps with another sprite.
There are a bunch of physics commands that you can use.
let mySprite;
function setup() {
createCanvas(400, 400);
mySprite=createSprite(200,50,25);
// note you can make gravity go down (y=1), up (y=-1),
// right (x=1) or left(x=-1)
// or increase the speed y=5;
world.gravity.y=5;
}
function draw() {
clear();
}
You can add static objects by setting the collider of the object = “static”
let mySprite;
let myFloor;
function setup() {
createCanvas(400, 400);
mySprite=createSprite(200,50,25);
myFloor=createSprite(200,360,400,40);
myFloor.collider="static";
world.gravity.y=5;
}
function draw() {
clear();
}
Sprites have properties that will control how they interact when they collide. “bounciness” will make the sprite bounce when it hits another object
let s1,s2,s3,s4;
let myFloor;
function setup() {
createCanvas(400, 400);
s1=createSprite(50,50,25);
s1.bounciness=0.25;
s2=createSprite(150,50,25);
s2.bounciness=0.5;
s3=createSprite(250,50,25);
s3.bounciness=1;
s4=createSprite(350,50,25);
s4.bounciness=1.5;
myFloor=createSprite(200,360,400,40);
myFloor.collider="static";
world.gravity.y=5;
}
function draw() {
clear();
}
Another property of an object is drag which determines how much friction there is when moving through space
let s1,s2,s3,s4;
let myFloor;
function setup() {
createCanvas(400, 400);
s1=createSprite(50,50,25);
s1.drag=0.25;
s2=createSprite(150,50,25);
s2.drag=0.5;
s3=createSprite(250,50,25);
s3.drag=1;
s4=createSprite(350,50,25);
s4.drag=2;
myFloor=createSprite(200,360,400,40);
myFloor.collider="static";
world.gravity.y=5;
}
function draw() {
clear();
}
let s1,s2,s3,s4;
let floor1,floor2;
function setup() {
createCanvas(400, 400);
s1=createSprite(50,100,25,25);
s1.friction=10;
s3=createSprite(250,100,25,25);
s3.friction=0;
floor1=createSprite(200,300,600,20);
floor1.rotation=10;
floor2=createSprite(500,300,600,20);
floor2.rotation=10;
floor1.collider="static";
floor2.collider="static";
world.gravity.y=5;
}
function draw() {
clear();
}