11using Stride . BepuPhysics ;
22using Stride . CommunityToolkit . Bepu ;
33using Stride . CommunityToolkit . Engine ;
4- using Stride . CommunityToolkit . Helpers ;
4+ using Stride . CommunityToolkit . Rendering . Gizmos ;
55using Stride . CommunityToolkit . Rendering . ProceduralModels ;
66using Stride . CommunityToolkit . Skyboxes ;
77using Stride . Core . Mathematics ;
88using Stride . Engine ;
99using Stride . Games ;
10+ using Stride . Graphics ;
1011using Stride . Input ;
12+ using Stride . Rendering ;
13+ using Buffer = Stride . Graphics . Buffer ;
1114
1215CameraComponent ? camera = null ;
16+ ModelComponent ? lineModelComponent = null ;
17+ Entity ? entity = null ;
18+ BodyComponent ? body = null ;
19+ Buffer ? vertexBuffer = null ;
20+ Vector3 [ ] vertices = new Vector3 [ 2 ] ; // Start and end points
21+ bool isMoving = false ;
1322
1423using var game = new Game ( ) ;
1524
@@ -19,44 +28,90 @@ void Start(Scene scene)
1928{
2029 game . SetupBase3DScene ( ) ;
2130 game . AddSkybox ( ) ;
31+ game . AddProfiler ( ) ;
2232 game . AddGroundGizmo ( new ( - 5 , 0 , - 5 ) , showAxisName : true ) ;
2333
24- var entity = game . Create3DPrimitive ( PrimitiveModelType . Sphere ) ;
25-
34+ entity = game . Create3DPrimitive ( PrimitiveModelType . Sphere ) ;
2635 entity . Transform . Position = new Vector3 ( 0 , 8 , 0 ) ;
27-
36+ body = entity . Get < BodyComponent > ( ) ;
2837 entity . Scene = scene ;
2938
3039 camera = scene . GetCamera ( ) ;
40+
41+ var lineEntity = CreateLineEntity ( game ) ;
42+
43+ entity . AddChild ( lineEntity ) ;
3144}
3245
3346void Update ( Scene scene , GameTime time )
3447{
3548 if ( camera == null ) return ;
3649
37- game . DebugTextSystem . Print ( "Click the sphere to apply a random impulse" , new ( 5 , 10 ) ) ;
50+ game . DebugTextSystem . Print ( "Click the ground to apply a direction impulse" , new ( 5 , 30 ) ) ;
51+ game . DebugTextSystem . Print ( "Click the sphere to stop moving" , new ( 5 , 50 ) ) ;
3852
3953 if ( game . Input . IsMouseButtonPressed ( MouseButton . Left ) )
4054 {
4155 var hit = camera . Raycast ( game . Input . MousePosition , 100 , out var hitInfo ) ;
4256
4357 if ( hit )
4458 {
45- var body = hitInfo . Collidable . Entity . Get < BodyComponent > ( ) ;
59+ Console . WriteLine ( $ "Hit entity: { hitInfo . Collidable . Entity . Name } " ) ;
4660
47- if ( body is null ) return ;
61+ if ( hitInfo . Collidable . Entity == entity && body != null )
62+ {
63+ body . LinearVelocity = Vector3 . Zero ;
64+ body . AngularVelocity = Vector3 . Zero ;
4865
49- var randomDirection = VectorHelper . RandomVector3 ( [ - 1 , 1 ] , [ 0 , 1 ] , [ - 1 , 1 ] ) ;
66+ return ;
67+ }
5068
51- Console . WriteLine ( $ "{ hitInfo . Collidable . Entity } , direction: { randomDirection } ") ;
69+ // ToDo: This point needs to be corrected, because lineEntity is a chidld of entity
70+ // Update the end vertex
71+ vertices [ 1 ] = hitInfo . Point ;
5272
53- body . ApplyImpulse ( randomDirection * 2 , new ( ) ) ;
73+ // Re-upload vertex data to GPU
74+ vertexBuffer ? . SetData ( game . GraphicsContext . CommandList , vertices ) ;
5475
76+ //Console.WriteLine($"{hitInfo.Collidable.Entity}, direction: {randomDirection}");
77+
78+ if ( body is null ) return ;
79+
80+ body . ApplyImpulse ( hitInfo . Point * 0.5f , new ( ) ) ;
5581 body . Awake = true ;
82+ isMoving = true ;
5683 }
5784 else
5885 {
5986 Console . WriteLine ( "No hit" ) ;
6087 }
6188 }
89+ }
90+
91+ Entity CreateLineEntity ( Game game )
92+ {
93+ // Initialize vertices (start at origin, end at origin)
94+ vertices [ 0 ] = Vector3 . Zero ;
95+ vertices [ 1 ] = Vector3 . One ;
96+
97+ // Create vertex buffer with start and end points
98+ vertexBuffer = Buffer . New ( game . GraphicsDevice , vertices , BufferFlags . VertexBuffer ) ;
99+
100+ // Create index buffer
101+ var indices = new ushort [ ] { 0 , 1 } ;
102+ var indexBuffer = Buffer . New ( game . GraphicsDevice , indices , BufferFlags . IndexBuffer ) ;
103+
104+ var meshDraw = new MeshDraw
105+ {
106+ PrimitiveType = PrimitiveType . LineList ,
107+ VertexBuffers = [ new VertexBufferBinding ( vertexBuffer , new VertexDeclaration ( VertexElement . Position < Vector3 > ( ) ) , vertices . Length ) ] ,
108+ IndexBuffer = new IndexBufferBinding ( indexBuffer , is32Bit : false , indices . Length ) ,
109+ DrawCount = indices . Length
110+ } ;
111+
112+ var mesh = new Mesh { Draw = meshDraw } ;
113+
114+ lineModelComponent = new ModelComponent { Model = new Model { mesh , GizmoEmissiveColorMaterial . Create ( game . GraphicsDevice , Color . DarkMagenta ) } } ;
115+
116+ return new Entity { lineModelComponent } ;
62117}
0 commit comments