Skip to content

Commit

Permalink
Merge pull request #706 from TheCreatorGrey/master
Browse files Browse the repository at this point in the history
Added missing physics shapes and fixed offset in procedural cone
  • Loading branch information
pokepetter authored Aug 21, 2024
2 parents ca0ac82 + 9f3efe9 commit 99902c7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
14 changes: 7 additions & 7 deletions ursina/models/procedural/cone.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def __init__(self, resolution=4, radius=.5, height=1, add_bottom=True, mode='tri

verts = []
for i in range(resolution):
verts.append(Vec3(v[0], 0, v[1]))
verts.append(Vec3(v[0], -(height/2), v[1]))
v = rotate_around_point_2d(v, origin, -degrees_to_rotate)
verts.append(Vec3(v[0], 0, v[1]))
verts.append(Vec3(v[0], -(height/2), v[1]))

verts.append(Vec3(0,height,0))
verts.append(Vec3(0,height/2,0))
if add_bottom:
for i in range(resolution):
verts.append(Vec3(v[0], 0, v[1]))
verts.append(Vec3(0,0,0))
verts.append(Vec3(v[0], 0-(height/2), v[1]))
verts.append(Vec3(0,-(height/2),0))
v = rotate_around_point_2d(v, origin, -degrees_to_rotate)
verts.append(Vec3(v[0], 0, v[1]))
verts.append(Vec3(v[0], -(height/2), v[1]))


super().__init__(vertices=verts, uvs=[e.xy for e in verts], mode=mode, **kwargs)
Expand All @@ -41,4 +41,4 @@ def __init__(self, resolution=4, radius=.5, height=1, add_bottom=True, mode='tri

origin = Entity(model='quad', color=color.orange, scale=(.05, .05))
ed = EditorCamera()
app.run()
app.run()
46 changes: 42 additions & 4 deletions ursina/physics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from panda3d.core import BitMask32, TransformState
from panda3d.bullet import BulletRigidBodyNode, BulletPlaneShape, BulletBoxShape, BulletSphereShape, BulletCapsuleShape, XUp, YUp, ZUp, BulletTriangleMesh, BulletTriangleMeshShape, BulletDebugNode, BulletWorld
from panda3d.bullet import BulletRigidBodyNode, BulletPlaneShape, BulletBoxShape, BulletSphereShape, BulletCapsuleShape, BulletCylinderShape, BulletConeShape, XUp, YUp, ZUp, BulletTriangleMesh, BulletTriangleMeshShape, BulletDebugNode, BulletWorld
from ursina.scripts.property_generator import generate_properties_for_class
from ursina import Entity, scene, time, Vec3

Expand Down Expand Up @@ -55,6 +55,20 @@ def __init__(self, center=(0,0,0), radius=.5, height=2, axis='y'):
self.height = height
self.axis = axis

class CylinderShape:
def __init__(self, center=(0,0,0), radius=.5, height=2, axis='y'):
self.center = center
self.radius = radius
self.height = height
self.axis = axis

class ConeShape:
def __init__(self, center=(0,0,0), radius=.5, height=2, axis='y'):
self.center = center
self.radius = radius
self.height = height
self.axis = axis

class MeshShape:
def __init__(self, mesh=None, center=(0,0,0)):
self.mesh = mesh
Expand Down Expand Up @@ -224,6 +238,24 @@ def _convert_shape(shape, entity, dynamic=True):
elif shape.axis == 'x':
axis = XUp
return BulletCapsuleShape(shape.radius, shape.height-1, axis)

elif isinstance(shape, CylinderShape):
if shape.axis == 'y':
axis = YUp
elif shape.axis == 'z':
axis = ZUp
elif shape.axis == 'x':
axis = XUp
return BulletCylinderShape(shape.radius, shape.height, axis)

elif isinstance(shape, ConeShape):
if shape.axis == 'y':
axis = YUp
elif shape.axis == 'z':
axis = ZUp
elif shape.axis == 'x':
axis = XUp
return BulletConeShape(shape.radius, shape.height, axis)

elif isinstance(shape, MeshShape):
if entity:
Expand All @@ -243,7 +275,7 @@ def _convert_shape(shape, entity, dynamic=True):


if __name__ == '__main__':
from ursina import Ursina, Capsule, Sequence, Func, curve, Wait, EditorCamera
from ursina import Ursina, Capsule, Cone, Cylinder, Sequence, Func, curve, Wait, EditorCamera

app = Ursina(borderless=False)

Expand All @@ -262,14 +294,20 @@ def _convert_shape(shape, entity, dynamic=True):
capsule = Entity(model=Capsule(height=2, radius=.5), texture='brick', y=17)
RigidBody(shape=CapsuleShape(height=2, radius=.5), entity=capsule, mass=3)

cylinder = Entity(model=Cylinder(height=2, radius=.5, start=-1), texture='brick', y=10)
RigidBody(shape=CylinderShape(height=2, radius=.5), entity=cylinder, mass=3)

cone = Entity(model=Cone(8, height=2, radius=.5), texture='brick', y=15)
RigidBody(shape=ConeShape(height=2, radius=.5), entity=cone, mass=2)

platform = Entity(model='cube', texture='white_cube', y=1, scale=(4,1,4))
platform_body = RigidBody(shape=BoxShape(), entity=platform, kinematic=True, friction=1)
platform_sequence = Sequence(entity=platform, loop=True)

path = [Vec3(-2,1,-2), Vec3(2,1,-2), Vec3(0, 1, 2)]
travel_time = 2
for target_pos in path:
platform_sequence.append(Func(platform_body.animate_position, value=target_pos, duration=travel_time, curve=curve.linear), regenerate=False)
platform_sequence.append(Func(platform_body.animate_position, value=target_pos, duration=travel_time, curve=curve.linear))
platform_sequence.append(Wait(travel_time))
platform_sequence.start()

Expand All @@ -284,4 +322,4 @@ def input(key):
physics_handler.debug = True

EditorCamera()
app.run()
app.run()

0 comments on commit 99902c7

Please sign in to comment.