Skip to content

Commit

Permalink
v. 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy16823 committed Feb 23, 2024
1 parent 79bc82b commit 0cd7561
Show file tree
Hide file tree
Showing 165 changed files with 9,967 additions and 12 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Source/.vs/Genesis/v17/.suo
Binary file not shown.
127 changes: 127 additions & 0 deletions Source/Genesis/Core/Behaviors/Physics2D/BufferedSpriteCollider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using BulletSharp;
using BulletSharp.Math;
using Genesis.Core.GameElements;
using Genesis.Graphics.Shapes;
using Genesis.Math;
using Genesis.Physics;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Genesis.Core.Behaviors.Physics2D
{
/// <summary>
/// Represents a behavior for creating a physics collider for 2D sprites using BulletSharp.
/// </summary>
public class BufferedSpriteCollider : Physics.PhysicsBehavior
{
/// <summary>
/// Gets or sets the rigid body associated with the collider.
/// </summary>
public RigidBody RigidBody { get; set; }

/// <summary>
/// Gets or sets the linear factor for the RigidBody's motion.
/// </summary>
public Vec3 LinearFactor { get; set; } = new Vec3(0, 0, 0);

/// <summary>
/// Gets or sets the angular factor for the RigidBody's rotation.
/// </summary>
public Vec3 AngularFactor { get; set; } = new Vec3(0, 0, 0);

/// <summary>
/// Gets the physics object associated with the collider.
/// </summary>
/// <returns>The rigid body associated with the collider.</returns>
public override object GetPhysicsObject()
{
return RigidBody;
}

/// <summary>
/// Gets the typed physics object associated with the collider.
/// </summary>
/// <typeparam name="T">Type of the physics object.</typeparam>
/// <returns>The typed rigid body associated with the collider.</returns>
public override T GetPhysicsObject<T>()
{
return (T)(object)RigidBody;
}

/// <summary>
/// Creates a rigid body for the collider.
/// </summary>
/// <param name="mass">The mass of the rigid body.</param>
/// <param name="handler">The physics handler for managing the collider.</param>
public void CreateRigidBody(float mass, PhysicHandler handler)
{
if (this.Parent.GetType() == typeof(BufferedSprite))
{
var bufferedSprite = (BufferedSprite)this.Parent;
CompoundShape compoundShape = new CompoundShape(true);

foreach(var deffinition in bufferedSprite.ShapeDeffinitions)
{
Box2DShape box2DShape = new Box2DShape(new Vector3(deffinition.sizeX, deffinition.sizeY, 0f) / 2);
BulletSharp.Math.Matrix boxtransform = BulletSharp.Math.Matrix.Translation(new Vector3(deffinition.locX, deffinition.locY, 0f));
compoundShape.AddChildShape(boxtransform, box2DShape);
}
RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(mass, null, compoundShape, compoundShape.CalculateLocalInertia(mass));
BulletSharp.Math.Matrix transform = BulletSharp.Math.Matrix.Translation(bufferedSprite.Location.ToBulletVec3());
Matrix startTransform = transform;
info.MotionState = new DefaultMotionState(startTransform);

RigidBody = new BulletSharp.RigidBody(info);
RigidBody.UserObject = this.Parent;
RigidBody.LinearFactor = this.LinearFactor.ToBulletVec3();
RigidBody.AngularFactor = this.AngularFactor.ToBulletVec3();
//RigidBody.CollisionFlags = CollisionFlags.StaticObject;

handler.ManageElement(this);
}
else
{
throw new InvalidOperationException("Invalid element for this Behavior");
}
}

/// <summary>
/// Placeholder for OnDestroy event. No implementation.
/// </summary>
public override void OnDestroy(Game game, GameElement parent)
{

}

/// <summary>
/// Placeholder for OnInit event. No implementation.
/// </summary>
public override void OnInit(Game game, GameElement parent)
{

}

/// <summary>
/// Placeholder for OnRender event. No implementation.
/// </summary>
public override void OnRender(Game game, GameElement parent)
{

}

/// <summary>
/// Placeholder for OnUpdate event. No implementation.
/// </summary>
public override void OnUpdate(Game game, GameElement parent)
{
Matrix transform = BulletSharp.Math.Matrix.Translation(Parent.Location.ToBulletVec3());
RigidBody.MotionState = new DefaultMotionState(transform);
RigidBody.Activate(true);
}
}
}
5 changes: 4 additions & 1 deletion Source/Genesis/Core/Behaviors/Physics2D/Rigidbody2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class Rigidbody2D : Physics.PhysicsBehavior
/// </summary>
public Vec3 AngularFactor { get; set; } = new Vec3(0, 1, 0);

public bool EnablePhysic { get; set; } = true;

/// <summary>
/// Creates a RigidBody with the specified mass using the provided PhysicHandler.
/// </summary>
Expand All @@ -53,6 +55,7 @@ public void CreateRigidbody(PhysicHandler handler, float mass)
RigidBody = new BulletSharp.RigidBody(info);
RigidBody.LinearFactor = this.LinearFactor.ToBulletVec3();
RigidBody.AngularFactor = this.AngularFactor.ToBulletVec3();
RigidBody.UserObject = this.Parent;
this.RigidBody.ApplyGravity();
handler.ManageElement(this);
}
Expand Down Expand Up @@ -105,7 +108,7 @@ public override void OnRender(Game game, GameElement parent)
/// <param name="parent">The parent game element associated with this behavior.</param>
public override void OnUpdate(Game game, GameElement parent)
{
if (this.RigidBody != null && this.RigidBody.InvMass > 0)
if (this.EnablePhysic && this.RigidBody != null && this.RigidBody.InvMass > 0)
{
Vector3 position = RigidBody.WorldTransform.Origin;
Vec3 newLocation = Utils.GetModelSpaceLocation(Parent, new Vec3(position.X, position.Y, position.Z));
Expand Down
89 changes: 89 additions & 0 deletions Source/Genesis/Core/GameElments/BufferedSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@

namespace Genesis.Core.GameElements
{
/// <summary>
/// Represents the definition of the shape for a sprite, including its location and size.
/// </summary>
public struct SpriteShapeDeffinition
{
public SpriteShapeDeffinition(Vec3 loc, Vec3 size)
{
this.locX = loc.X;
this.locY = loc.Y;
this.sizeX = size.X;
this.sizeY = size.Y;
}

public float locX;
public float locY;
public float sizeX;
public float sizeY;
}


/// <summary>
/// Represents a game element that creates a buffered sprite with vertices, colors, and texture coordinates.
/// </summary>
Expand All @@ -29,6 +49,11 @@ public class BufferedSprite : GameElement
/// </summary>
public List<float> TexCoords { get; set; }

/// <summary>
/// Gets or sets the list of shape definitions for the sprite.
/// </summary>
public List<SpriteShapeDeffinition> ShapeDeffinitions { get; set; }

/// <summary>
/// Gets or sets the texture applied to the sprite.
/// </summary>
Expand All @@ -48,6 +73,7 @@ public BufferedSprite(String name, Vec3 location, Texture texture)
this.Verticies = new List<float>();
this.Colors = new List<float>();
this.TexCoords = new List<float>();
this.ShapeDeffinitions = new List<SpriteShapeDeffinition>();
}

/// <summary>
Expand Down Expand Up @@ -98,6 +124,69 @@ public void AddShape(Vec3 location, Vec3 size)
};
this.TexCoords.AddRange(textCoordsf);

SpriteShapeDeffinition deffinition = new SpriteShapeDeffinition();
deffinition.locX = location.X;
deffinition.locY = location.Y;
deffinition.sizeX = size.X;
deffinition.sizeY = size.Y;
this.ShapeDeffinitions.Add(deffinition);
}

/// <summary>
/// Adds a new rectangular shape at the given location, size, and texture coordinates to the sprite.
/// </summary>
/// <param name="location">The location for the sprite.</param>
/// <param name="size">The size for the sprite.</param>
/// <param name="texCoords">The texture coordinates for the sprite.</param>
public void AddShape(Vec3 location, Vec3 size, TexCoords texCoords)
{
float LeftX = location.X - (size.X / 2);
float RightX = location.X + (size.X / 2);
float top = location.Y + (size.Y / 2);
float bottom = location.Y - (size.Y / 2);

float[] verticies =
{
LeftX, bottom, 0.0f,
LeftX, top, 0.0f,
RightX, top, 0.0f,

LeftX, bottom, 0.0f,
RightX, top, 0.0f,
RightX, bottom, 0.0f
};
this.Verticies.AddRange(verticies);

float[] color =
{
1f, 1f, 1f,
1f, 1f, 1f,
1f, 1f, 1f,

1f, 1f, 1f,
1f, 1f, 1f,
1f, 1f, 1f
};
this.Colors.AddRange(color);

float[] textCoordsf =
{
texCoords.BottomLeft.X, texCoords.BottomLeft.Y, // Left bottom
texCoords.TopLeft.X, texCoords.TopLeft.Y,
texCoords.TopRight.X, texCoords.TopRight.Y,

texCoords.BottomLeft.X, texCoords.BottomLeft.Y,
texCoords.TopRight.X, texCoords.TopRight.Y,
texCoords.BottomRight.X,texCoords.BottomRight.Y
};
this.TexCoords.AddRange(textCoordsf);

SpriteShapeDeffinition deffinition = new SpriteShapeDeffinition();
deffinition.locX = location.X;
deffinition.locY = location.Y;
deffinition.sizeX = size.X;
deffinition.sizeY = size.Y;
this.ShapeDeffinitions.Add(deffinition);
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions Source/Genesis/Genesis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="Core\AssetManager.cs" />
<Compile Include="Core\Behaviors\AnimationBehavior.cs" />
<Compile Include="Core\Behaviors\AnimationBehavior3D.cs" />
<Compile Include="Core\Behaviors\Physics2D\BufferedSpriteCollider.cs" />
<Compile Include="Core\Behaviors\Physics2D\Rigidbody2D.cs" />
<Compile Include="Core\Behaviors\Physics3D\QubeConvexHull.cs" />
<Compile Include="Core\Behaviors\Physics3D\StaticMeshBehavior.cs" />
Expand Down Expand Up @@ -189,13 +190,13 @@
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets" Condition="Exists('..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets'))" />
<Error Condition="!Exists('..\packages\AssimpNet.4.1.0\build\AssimpNet.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\AssimpNet.4.1.0\build\AssimpNet.targets'))" />
<Error Condition="!Exists('..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets'))" />
</Target>
<Import Project="..\packages\AssimpNet.4.1.0\build\AssimpNet.targets" Condition="Exists('..\packages\AssimpNet.4.1.0\build\AssimpNet.targets')" />
<Import Project="..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets" Condition="Exists('..\packages\BulletSharp.0.11.1\build\net40-client\BulletSharp.targets')" />
</Project>
6 changes: 6 additions & 0 deletions Source/Genesis/Genesis.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
5 changes: 5 additions & 0 deletions Source/Genesis/Physics/PhysicsHandler2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public override void Process(Scene scene, Game game)
{
Callbacks[obA](scene, game, obB);
}

if(Callbacks.ContainsKey(obB))
{
Callbacks[obB](scene, game, obA);
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Source/Genesis/UI/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,5 @@ private Rect GetStringBounds(Vec3 location)
rect.Height = FontSize;
return rect;
}

}
}
Binary file modified Source/Genesis/bin/Debug/Genesis.dll
Binary file not shown.
Binary file modified Source/Genesis/bin/Debug/Genesis.pdb
Binary file not shown.
Loading

0 comments on commit 0cd7561

Please sign in to comment.