Skip to content

Commit 6d01338

Browse files
committed
make character stick to platform
1 parent 6c012bf commit 6d01338

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

examples/canvas/character.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,22 @@
121121

122122

123123
// Allow pass through platforms from below
124-
var passThroughBody;
124+
var passThroughBody, currentPlatform;
125125

126126
world.on('beginContact', function (evt){
127127
if(evt.bodyA !== characterBody && evt.bodyB !== characterBody) return;
128128
var otherBody = evt.bodyA === characterBody ? evt.bodyB : evt.bodyA;
129-
if(platforms.indexOf(otherBody) !== -1 && otherBody.position[1] > characterBody.position[1]){
129+
var platformIndex = platforms.indexOf(otherBody);
130+
if(platformIndex != -1 && otherBody.position[1] > characterBody.position[1]){
130131
passThroughBody = otherBody;
132+
} else if(platformIndex != -1){
133+
currentPlatform = platforms[platformIndex];
131134
}
132135
});
133136

134137
// Disable any equations between the current passthrough body and the character
135138
world.on('preSolve', function (evt){
139+
if(currentPlatform) characterBody.velocity[0] += currentPlatform.velocity[0];
136140
for(var i=0; i<evt.contactEquations.length; i++){
137141
var eq = evt.contactEquations[i];
138142
if((eq.bodyA === characterBody && eq.bodyB === passThroughBody) || eq.bodyB === characterBody && eq.bodyA === passThroughBody){
@@ -151,6 +155,12 @@
151155
if((evt.bodyA === characterBody && evt.bodyB === passThroughBody) || evt.bodyB === characterBody && evt.bodyA === passThroughBody){
152156
passThroughBody = undefined;
153157
}
158+
if(evt.bodyA !== characterBody && evt.bodyB !== characterBody) return;
159+
var otherBody = evt.bodyA === characterBody ? evt.bodyB : evt.bodyA;
160+
var platformIndex = platforms.indexOf(otherBody);
161+
if(otherBody == currentPlatform){
162+
currentPlatform = null;
163+
}
154164
});
155165

156166
}

0 commit comments

Comments
 (0)