Support physic on non-convex shape #3753
Replies: 5 comments
-
@4ian can we make a road map to improve collision polygon with this solution? decompose-polygon ts version |
Beta Was this translation helpful? Give feedback.
-
Check out the idea and we can start working from this line: https://github.com/4ian/GDevelop/blob/master/Extensions/Physics2Behavior/physics2runtimebehavior.ts#L473-L547 Just need to see how to change it: |
Beta Was this translation helpful? Give feedback.
-
@jjhesk Please rather discuss your ideas on the forum first - I know that you're doing a lot of research but you need to consolidate these and get ideas/feedbacks from other users before posting on GitHub (unfortunately posting multiple links is not super useful by itself - the research needs to be much more precise, or even prototyped if you can :)). |
Beta Was this translation helpful? Give feedback.
-
To create a plugin for GDevelop that calculates a collision polygon for curve shapes to enable collision detection with a circle shape, we need a clear, practical approach that integrates with GDevelop’s framework. The plugin will allow users to define curve shapes (e.g., Bézier curves or other parametric curves) and generate a collision polygon to detect intersections with a circular object. Below, I outline the concept, technical approach, and implementation steps for developing this plugin, leveraging insights from the provided GitHub discussion and relevant collision detection techniques. Concept OverviewThe plugin will:
The GitHub discussion (#3753) highlights issues with GDevelop’s physics engine and resource handling, suggesting that plugins must be robust to avoid errors like missing resources or undefined properties. Thus, the plugin will include error handling and clear documentation. Technical Approach
Implementation StepsBelow are the steps to develop the plugin, assuming familiarity with JavaScript (used for GDevelop extensions) and basic collision detection math. Step 1: Set Up the Plugin
Step 2: Define the Curve Shape
Step 3: Approximate the Curve as a Polygon
Step 4: Implement Circle-Polygon Collision Detection
Step 5: Integrate Collision Detection in GDevelop
Step 6: Visualization and Debugging
Step 7: Test and Optimize
|
Beta Was this translation helpful? Give feedback.
-
Example Code Snippet (Pseudo-JavaScript)Below is a simplified example of the collision detection logic, which would be part of the extension’s runtime code: gdjs.registerObject("CurveCollisionExtension::CurveShape", function(runtimeScene) {
var CurveShape = function() {
this.controlPoints = [{x: 0, y: 0}, {x: 100, y: 50}, {x: 200, y: 50}, {x: 300, y: 0}];
this.segmentCount = 20;
this.polygon = [];
this.updatePolygon();
};
CurveShape.prototype.updatePolygon = function() {
this.polygon = [];
for (var t = 0; t <= 1; t += 1 / this.segmentCount) {
var point = this.evaluateBezier(t); // Implement Bézier evaluation
this.polygon.push(point);
}
};
CurveShape.prototype.isCollidingWithCircle = function(circleObject, radius) {
let circleCenter = { x: circleObject.getX(), y: circleObject.getY() };
// Check edge intersections
for (let i = 0; i < this.polygon.length - (this.isClosed ? 0 : 1); i++) {
let A = this.polygon[i];
let B = this.polygon[(i + 1) % this.polygon.length]; // Loop back for closed
let AB = { x: B.x - A.x, y: B.y - A.y };
let AC = { x: circleCenter.x - A.x, y: circleCenter.y - A.y };
let lengthSquared = AB.x * AB.x + AB.y * AB.y;
let t = Math.max(0, Math.min(1, (AC.x * AB.x + AC.y * AB.y) / lengthSquared));
let closest = { x: A.x + t * AB.x, y: A.y + t * AB.y };
let distance = Math.sqrt(
(circleCenter.x - closest.x) ** 2 + (circleCenter.y - closest.y) ** 2
);
if (distance <= radius) return true;
}
// Check if circle center is inside (for closed polygons)
if (this.isClosed && isPointInPolygon(circleCenter, this.polygon)) {
return true;
}
return false;
};
} The plugin concept I outlined for GDevelop, which approximates a curve as a polygon for collision detection with a circle, does not inherently support concave polygons in its basic implementation. However, it can be extended to handle concave polygons with additional logic. Below, I explain why concave polygons require special consideration, whether the proposed approach supports them, and how to modify the plugin to include concave polygon support. Why Concave Polygons Are a Challenge
Current Plugin Support for Concave PolygonsThe plugin, as described, supports concave polygons in the following sense:
Extending the Plugin to Support Concave PolygonsTo fully support concave polygons, including closed curves where the circle might be inside the shape, you can enhance the plugin with the following modifications: 1. Add Support for Closed Curves
2. Implement Point-in-Polygon Testing
3. Enhance Collision Detection
1. Optimize for Performance
2. Handle Self-Intersecting Curves
3. Test Concave Cases
Practical Considerations
Example Use CaseImagine a GDevelop game with a concave, star-shaped barrier (defined as a closed Bézier curve) and a circular player object:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have read about the content on wiki which talking about:
I think it is possible to calculate custom shape like the moon shape which can save tons of time in work-arounds done by hand.

Found some resources about this solution:
filling technique: https://tronche.com/gui/x/xlib/graphics/filling-areas/XFillPolygon.html
research
from godot documentation
from more discussions
Concave Polygons
Collisions uses the Separating Axis Theorem (SAT) for its narrow-phase collision tests. One caveat to SAT is that it only works properly on convex bodies. However, concave polygons can be "faked" by using a series of Lines. Keep in mind that a polygon drawn using Lines is "hollow".
Handling true concave polygons requires breaking them down into their component convex polygons (Convex Decomposition) and testing them for collisions individually. There are plans to integrate this functionality into the library in the future, but for now, check out poly-decomp.js.
Beta Was this translation helpful? Give feedback.
All reactions