Can you demonstrate a simple example of how we can configure the bounciness and friction of objects that can collide in a scene using scripting ? Avoid pseudo code.
Hi Enthusiast, here’s a concrete example in TypeScript that demonstrates how to assign a physics material to a collider (in this case a BoxCollider) to control properties like bounciness and friction. In Needle Engine you can attach this component to an object that has a collider, and the script will set up the physics material using Rapier under the hood. See below:
import { Behaviour, serializable, BoxCollider } from "@needle-tools/engine";
import { PhysicsMaterial, PhysicsMaterialCombine } from "@needle-tools/engine";
export class PhysicsMaterialAssigner extends Behaviour {
// Define the bounciness and friction settings
@serializable() bounciness: number = 0.8;
@serializable() dynamicFriction: number = 0.3;
// Enum settings to control how friction and bounce values are combined during collisions.
@serializable() frictionCombine: PhysicsMaterialCombine = PhysicsMaterialCombine.Average;
@serializable() bounceCombine: PhysicsMaterialCombine = PhysicsMaterialCombine.Maximum;
// Optionally, reference a specific collider (e.g. BoxCollider) from your scene.
// If none is set in the inspector, the script will search the same GameObject for one.
@serializable(BoxCollider)
collider?: BoxCollider;
awake() {
// Get the assigned collider or try to get one from the GameObject.
const col = this.collider || this.gameObject.getComponent(BoxCollider);
if (!col) {
console.warn("No collider found on the object for physics material assignment.");
return;
}
// Create and assign the physics material with the configured bounciness and friction.
col.physicsMaterial = {
bounciness: this.bounciness,
dynamicFriction: this.dynamicFriction,
frictionCombine: this.frictionCombine,
bounceCombine: this.bounceCombine
} as PhysicsMaterial;
console.log("Physics material assigned:", col.physicsMaterial);
}
}
In this example the component:
• Exposes properties for bounciness and dynamic friction that you can adjust in the editor.
• Uses the enums from PhysicsMaterialCombine (for which you can choose between Average, Multiply, Minimum, Maximum – see at engine_physics.types.d.ts.md) to set how friction and bounce should be combined.
• Attempts to get a BoxCollider on the current GameObject if one isn’t manually assigned.
Attach this Behaviour to any GameObject that includes a collider, and when the scene starts the assigned physics material will dictate how the object behaves on collision.
This should give you a clear, working example of using scripting to configure physics materials in Needle Engine. Let me know if you need any further details!
4 replies left
thread_yJmPlJ21aVJkzqUMGB2mC01D
Reply to this post directly if you need further help (bottom right button)
This code produces an error:
Error Property 'physicsMaterial' does not exist on type 'BoxCollider'.
Hi Enthusiast, the error occurs because BoxCollider doesn’t expose a property named physicsMaterial. Instead, colliders in Needle Engine use a property called sharedMaterial to store their physics material settings. You can assign your bounciness and friction configurations to sharedMaterial and then call updatePhysicsMaterial() so that the new settings take effect.
Here’s an updated example:
import { Behaviour, serializable, BoxCollider, PhysicsMaterialCombine } from “@needle-tools/engine”;
import type { PhysicsMaterial } from “@needle-tools/engine”;
export class PhysicsMaterialAssigner extends Behaviour {
@serializable() bounciness: number = 0.8;
@serializable() dynamicFriction: number = 0.3;
// Enum settings to control how forces are combined
@serializable() frictionCombine: PhysicsMaterialCombine = PhysicsMaterialCombine.Average;
@serializable() bounceCombine: PhysicsMaterialCombine = PhysicsMaterialCombine.Maximum;
awake() {
const col = this.gameObject.getComponent(BoxCollider);
if (!col) {
console.warn(“No collider found on the object.”);
return;
}
// Assign your physics material settings to sharedMaterial (not physicsMaterial)
col.sharedMaterial = {
bounciness: this.bounciness,
dynamicFriction: this.dynamicFriction,
frictionCombine: this.frictionCombine,
bounceCombine: this.bounceCombine
} as PhysicsMaterial;
// Make sure to update the collider so the new material takes effect:
col.updatePhysicsMaterial();
console.log("Updated physics material:", col.sharedMaterial);
}
}
In this updated code the physics configuration is set on sharedMaterial and then updatePhysicsMaterial() is called, as advised by the interface from Needle Engine (see at engine_types.d.ts.md). This should resolve the error you encountered. Let me know if you need any further assistance!
3 replies left
thread_yJmPlJ21aVJkzqUMGB2mC01D
Reply to this post directly if you need further help (bottom right button)
@needleEnthusiast here’s a minimal example: Needle Engine Bouncing Ball - StackBlitz
collider.sharedMaterial = {
bounciness: 1,
bounceCombine: PhysicsMaterialCombine.Maximum,
};
This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.