Skip to content

Instantly share code, notes, and snippets.

@michaliskambi
Created November 6, 2023 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaliskambi/067414b28ab3b14ea4c19e072a927a23 to your computer and use it in GitHub Desktop.
Save michaliskambi/067414b28ab3b14ea4c19e072a927a23 to your computer and use it in GitHub Desktop.
Beginning of a custom TCastleMeshCollider descendant that can read information from PhysicsEditor PLIST file
{ Sketch of a custom TCastleMeshCollider descendant,
that can read the information
from https://github.com/CodeAndWeb/PhysicsEditor-Loaders/ .
Consider it public domain, do with it whatever you want,
I hope it will help any contributor interested in implementing
https://github.com/CodeAndWeb/PhysicsEditor-Loaders/issues/17 . }
unit CastleColliderFromPhysicsEditor;
interface
uses CastleTransform, CastleScene;
type
TCastleColliderFromPhysicsEditor = class(TCastleMeshCollider)
strict private
FUrl: String;
FInternalMesh: TCastleScene;
procedure SetUrl(const Value: string);
published
property Url: string read FUrl write SetUrl;
end;
implementation
uses Dom, SysUtils,
CastleXmlUtils, X3DNodes, CastleVectors;
procedure TCastleColliderFromPhysicsEditor.SetUrl(const Value: string);
var
Doc: TXmlDocument;
Coord: TCoordinateNode;
IndexedTriangleSet: TIndexedTriangleSetNode;
Shape: TShapeNode;
RootNode: TX3DRootNode;
begin
if FUrl <> Value then
begin
FUrl := Value;
{ Recreate FInternalMesh and assign it to ancesotr's Mesh property }
Doc := UrlReadXml(Value);
try
{ TODO: Parse information from Doc.
For a sample how to read data from PLIST (a special case of XML)
you can take a look at X3DLoadInternalCocos2d unit in CGE,
see src/scene/load/x3dloadinternalcocos2d.pas in CGE sources.
For now, code below just creates a simple triangle
from 3 3D points. }
FreeAndNil(FInternalMesh);
{ Create a set of nodes and TCastleScene that describes the collider
shape. Based on
https://castle-engine.io/viewport_and_scenes_from_code#_building_a_mesh_using_code .
Note that the FInternalMesh will be never really visible,
so we don't care about assigning any fancy material.
}
Coord := TCoordinateNode.Create;
Coord.SetPoint([
Vector3(-15.205387, -66.775894, -0.092525),
Vector3(9.317978, -66.775894, -0.092525),
Vector3(-15.205387, -68.674622, -0.092525)
]);
IndexedTriangleSet := TIndexedTriangleSetNode.Create;
IndexedTriangleSet.Coord := Coord;
IndexedTriangleSet.SetIndex([0, 1, 2]);
Shape := TShapeNode.Create;
Shape.Geometry := IndexedTriangleSet;
RootNode := TX3DRootNode.Create;
RootNode.AddChildren(Shape);
FInternalMesh := TCastleScene.Create(Self);
FInternalMesh.Load(RootNode, true);
Mesh := FInternalMesh;
finally FreeAndNil(Doc) end;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment