how to do this : we
5:47
could just make a small solid edge
5:49
around the portal to stand on as long as
5:51
we're inside the portal the only thing
5:53
we can collide with is the outer edges
5:54
of the portal and this allows us to
5:57
stand halfway inside a wall without ever
5:59
colliding with that wall it's a cheap
6:01
trick but as far as I can see it just
6:03
works so good in here : class World {
constructor() {
this.game = new Game()
this.scene = this.game.scene
this.physics = this.initPhysics()
this.physics.gravity.set(0, -20, 0)
this.defaultMaterial = new CANNON.Material('default')
this.raycaster = new THREE.Raycaster()
this.mouse = new THREE.Vector2()
this.leftPortal = []
this.rightPortal = []
this.portalPhysics = false
this.collisionDetected = false
this.virtualCamera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
this.scene.add(this.virtualCamera)
this.collisionDetected = false
this.portalHandler = new Portal(this.scene, this.game.renderer.instance)
this.active = ''
this.previousContacts = []
this.worldBounds = {
min: new CANNON.Vec3(-100, -100, -100),
max: new CANNON.Vec3(100, 100, 100)
}
const defaultContactMaterial = new CANNON.ContactMaterial(
this.defaultMaterial,
this.defaultMaterial,
{
friction: 0,
restitution: 0
}
)
this.physics.defaultContactMaterial = defaultContactMaterial
window.addEventListener('click', this.shoot.bind(this))
code
this.isHoldingCube = false
this.holdDistance = 10
this.pickupRange = 20
this.holdingConstraint = null
this.holdBody = null
this.isJumping = true;
this.onKeyDown = this.onKeyDown.bind(this)
window.addEventListener('keydown', this.onKeyDown)
this.setWorld()
this.createCompanionCube()
this.loadTextures()
let upVector = new CANNON.Vec3(0, 1, 0);
let contactNormal = new CANNON.Vec3(0, 0, 0);
this.physics.addEventListener("postStep", (e) => {
this.isJumping = true; // Assume jumping until proven otherwise
if (this.physics.contacts.length > 0) {
for (let contact of this.physics.contacts) {
if (contact.bi.class == 'companionCube' || contact.bj.class == 'companionCube') {
if (contact.bi.class == 'companionCube') {
contactNormal = new CANNON.Vec3().copy(contact.ni).scale(-1);
} else {
contactNormal = new CANNON.Vec3().copy(contact.ni);
}
const collisionResponse = contact.bi.collisionResponse && contact.bj.collisionResponse;
this.isJumping = !(collisionResponse && contactNormal.dot(upVector) > 0.5);
}
}
}
});
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
if (distance <= this.pickupRange) {
this.isHoldingCube = true
const holdPosition = this.getHoldPosition()
this.holdBody = new CANNON.Body({
mass: 0,
type: CANNON.Body.KINEMATIC,
collisionFilterGroup: 0,
collisionFilterMask: 0
})
this.holdBody.position.copy(holdPosition)
this.physics.addBody(this.holdBody)
this.holdingConstraint = new CANNON.PointToPointConstraint(
this.companionCubeBody,
new CANNON.Vec3(0, 0, 0),
this.holdBody,
new CANNON.Vec3(0, 0, 0),
1e6
)
this.physics.addConstraint(this.holdingConstraint)
this.companionCubeBody.angularDamping = 1
// Add this line to remove collision with the player
this.companionCubeBody.collisionFilterMask &= ~GROUP_PLAYER
}
}
dropCube() {
this.isHoldingCube = false
code
if (this.holdingConstraint) {
this.physics.removeConstraint(this.holdingConstraint)
this.holdingConstraint = null
}
if (this.holdBody) {
this.physics.removeBody(this.holdBody)
this.holdBody = null
}
// Add the player group back to the collision mask
this.companionCubeBody.collisionFilterMask |= GROUP_PLAYER
this.companionCubeBody.angularDamping = 0.5
To achieve the effect described in the video—allowing the player to stand halfway inside a wall when inside the portal by colliding only with a small solid edge around the portal—you'll need to adjust your collision handling logic in your World class. Specifically, you'll create a small collision body (representing the portal's edges) that the player can collide with, and adjust collision filters to prevent the player from colliding with the wall where the portal is placed.
Here's how you can implement this step by step:
1. Define Collision Groups
First, ensure you have defined collision groups to manage collision behavior between different objects. From your code, it looks like you have collision groups like GROUP_WALL, GROUP_PLAYER, GROUP_PORTAL, etc. If not already defined, you can define them as follows:
When you create the portal in your shoot() method, add a new collision body that represents the small solid edge around the portal. This edge will be a thin border that the player can collide with.
Modify your portal creation code as follows:
javascript
// Existing code to create the portal body (boxBody)
// ...
// Create the portal edge body
const edgeThickness = 0.1; // Adjust as needed
const portalWidth = 3.34; // Half-width of your portal
const portalHeight = 5; // Half-height of your portal
// Create shapes for the edges
const edgeShapes = [];
// Top edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(portalWidth, edgeThickness, edgeThickness)));
// Bottom edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(portalWidth, edgeThickness, edgeThickness)));
// Left edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(edgeThickness, portalHeight, edgeThickness)));
// Right edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(edgeThickness, portalHeight, edgeThickness)));
this.portalEdgeBody = new CANNON.Body({
collisionFilterGroup: GROUP_PORTAL_EDGE,
collisionFilterMask: GROUP_PLAYER,
mass: 0,
material: this.defaultMaterial,
});
const positions = [
new CANNON.Vec3(0, portalHeight, 0), // Top edge
new CANNON.Vec3(0, -portalHeight, 0), // Bottom edge
new CANNON.Vec3(-portalWidth, 0, 0), // Left edge
new CANNON.Vec3(portalWidth, 0, 0), // Right edge
];
edgeShapes.forEach((shape, index) => {
this.portalEdgeBody.addShape(shape, positions[index]);
});
// Position and orient the portal edge body
this.portalEdgeBody.position.copy(this.boxBody.position);
this.portalEdgeBody.quaternion.copy(this.boxBody.quaternion);
// Add the portal edge body to the physics world
this.physics.addBody(this.portalEdgeBody);
// Keep a reference to the portal edge body
circle.portalEdgeBody = this.portalEdgeBody;
In the above code:
We create four thin CANNON.Box shapes representing the edges of the portal.
We position these shapes around the portal to form a frame.
The portalEdgeBody is set to only collide with the player (GROUP_PLAYER), ensuring that only the player can collide with the portal edges.
3. Adjust Wall Collision Filtering
To prevent the player from colliding with the wall where the portal is placed, you'll need to modify the wall's collision mask for the area covered by the portal.
Since modifying the wall's collision shape is complex, we can adjust the collision filtering when the portal is created. Here's how:
javascript
// In your portal creation code, after positioning the portal
const wallBody = object.physicObject; // The wall the portal is placed on
// Modify the wall's collisionFilterMask to exclude the player where the portal is
if (!wallBody.originalCollisionFilterMask) {
wallBody.originalCollisionFilterMask = wallBody.collisionFilterMask;
}
wallBody.collisionFilterMask &= ~GROUP_PLAYER;
This code temporarily adjusts the wall's collision filter to exclude collisions with the player. We also store the original collision filter mask so we can restore it later.
4. Restore Wall Collision Filtering When Portal is Removed
When a portal is removed or replaced, ensure you restore the wall's collision filter mask:
javascript
// In your code where you remove portals (e.g., in managePortals())
if (this.leftPortal.length > 1) {
const circleToRemove = this.leftPortal.shift();
// Restore wall collision filter
const wallBody = circleToRemove.physicObject.object.physicObject;
if (wallBody.originalCollisionFilterMask !== undefined) {
wallBody.collisionFilterMask = wallBody.originalCollisionFilterMask;
delete wallBody.originalCollisionFilterMask;
}
// Remove portal edge body
this.physics.removeBody(circleToRemove.portalEdgeBody);
// Remove other portal elements
// ...
}
Repeat similar steps for the right portal.
5. Update Collision Filters During Teleportation
When the player is teleporting through the portal, you might need to adjust collision filters to prevent unwanted collisions. Ensure that during teleportation, the player's collision mask is correctly set.
6. Adjust Player's Collision Mask When Inside the Portal
Optionally, you can adjust the player's collision mask when they are inside the portal to prevent collisions with the walls completely. However, handling it via the portal edge bodies and wall collision masks is usually sufficient.
7. Update the Physics Step and Collision Handling
Ensure that during your physics update step, you handle the collision responses appropriately. Since we've introduced new collision bodies (portalEdgeBody), make sure they are updated correctly.
8. Test and Adjust as Necessary
Test the implementation to ensure the player can stand halfway inside the wall when inside the portal.
Ensure that the player collides with the portal's edges and not the wall behind it.
Adjust the edgeThickness, and collision filters if necessary to get the desired effect.
Full Code Modification
Here's how your shoot() method might look after these modifications:
javascript
shoot(event) {
// Existing code to find intersection and create portal mesh (circle)
// ...
// Existing code to create the portal physics body (boxBody)
// ...
// Create the portal edge body
const edgeThickness = 0.1; // Adjust as needed
const portalWidth = 3.34; // Half-width of your portal shape
const portalHeight = 5; // Half-height of your portal shape
// Create shapes for the edges
const edgeShapes = [];
// Top edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(portalWidth, edgeThickness, edgeThickness)));
// Bottom edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(portalWidth, edgeThickness, edgeThickness)));
// Left edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(edgeThickness, portalHeight, edgeThickness)));
// Right edge
edgeShapes.push(new CANNON.Box(new CANNON.Vec3(edgeThickness, portalHeight, edgeThickness)));
const portalEdgeBody = new CANNON.Body({
collisionFilterGroup: GROUP_PORTAL_EDGE,
collisionFilterMask: GROUP_PLAYER,
mass: 0,
material: this.defaultMaterial,
});
const positions = [
new CANNON.Vec3(0, portalHeight, 0), // Top edge
new CANNON.Vec3(0, -portalHeight, 0), // Bottom edge
new CANNON.Vec3(-portalWidth, 0, 0), // Left edge
new CANNON.Vec3(portalWidth, 0, 0), // Right edge
];
edgeShapes.forEach((shape, index) => {
portalEdgeBody.addShape(shape, positions[index]);
});
portalEdgeBody.position.copy(this.boxBody.position);
portalEdgeBody.quaternion.copy(this.boxBody.quaternion);
this.physics.addBody(portalEdgeBody);
// Store reference to portal edge body
circle.portalEdgeBody = portalEdgeBody;
// Adjust wall's collisionFilterMask
const wallBody = object.physicObject; // The wall the portal is on
if (!wallBody.originalCollisionFilterMask) {
wallBody.originalCollisionFilterMask = wallBody.collisionFilterMask;
}
wallBody.collisionFilterMask &= ~GROUP_PLAYER;
// Existing code to create portal visuals and add to scene
// ...
}
And in your managePortals() method:
javascript
managePortals() {
// Existing code for leftPortal
if (this.leftPortal.length > 1) {
const circleToRemove = this.leftPortal.shift();
// Restore wall collision filter
const wallBody = circleToRemove.physicObject.object.physicObject;
if (wallBody.originalCollisionFilterMask !== undefined) {
wallBody.collisionFilterMask = wallBody.originalCollisionFilterMask;
delete wallBody.originalCollisionFilterMask;
}
// Remove portal edge body
this.physics.removeBody(circleToRemove.portalEdgeBody);
// Remove other portal elements
this.scene.remove(circleToRemove.box);
this.scene.remove(circleToRemove.torus);
this.physics.removeBody(circleToRemove.physicObject);
this.scene.remove(circleToRemove);
}
// Repeat for rightPortal
if (this.rightPortal.length > 1) {
const circleToRemove = this.rightPortal.shift();
// Restore wall collision filter
const wallBody = circleToRemove.physicObject.object.physicObject;
if (wallBody.originalCollisionFilterMask !== undefined) {
wallBody.collisionFilterMask = wallBody.originalCollisionFilterMask;
delete wallBody.originalCollisionFilterMask;
}
// Remove portal edge body
this.physics.removeBody(circleToRemove.portalEdgeBody);
// Remove other portal elements
this.scene.remove(circleToRemove.box);
this.scene.remove(circleToRemove.torus);
this.physics.removeBody(circleToRemove.physicObject);
this.scene.remove(circleToRemove);
}
}
Notes
Collision Filters: The collisionFilterGroup and collisionFilterMask properties are crucial in controlling which bodies can collide. Make sure these are correctly set for all bodies involved.
Edge Thickness: The edgeThickness should be small enough to allow smooth movement but thick enough to prevent the player from slipping through.
Performance: Adding more collision bodies may impact performance. Test and optimize as needed.
Testing: Thoroughly test the interaction, including edge cases like the player moving quickly into the portal, to ensure collision behaves correctly.
Conclusion
By creating a small solid edge around the portal and adjusting collision filters appropriately, you allow the player to move halfway into the wall without colliding with it, only interacting with the portal edges. This method effectively replicates the "cheap trick" described in the video while maintaining consistent collision behavior in your game.
Let me know if you need further clarification or assistance with implementing this solution!