Exemple #1
0
def logger(name, scene, period, total):
    if scene.objects["ball"].matrix[3][2] < 0:
        # Do not continue simulation if we hit the ground.
        scene.clocks[name].kill()  # We do not need this clock anymore
        return None

    # Log ball location
    logging.debug("Ball position: x:{} y:{} z:{} t={}".format(
        scene.objects["ball"].matrix[3][0],
        scene.objects["ball"].matrix[3][1],
        scene.objects["ball"].matrix[3][2],
        total,
    ))


#  Definitions
pm_scene = Scene()

ball = Sphere(radius=1, track_motion=True)

# Add ball to the scene
pm_scene.add_object("ball", ball)
pm_scene.observers[0].target_object = ball  # Track the ball

pm_scene.grid.resize(30, 30, 2)

pm_scene.create_clock("motion", 0.01, projectile_motion)
pm_scene.create_clock("logger", 0.05, logger)

pm_scene.run()
Exemple #2
0
import os
from payton.scene import Scene
from payton.scene.geometry import Mesh


scene = Scene()
mesh = Mesh()
mesh.add_triangle(
    [[0, 0, 0], [2, 0, 0], [2, 2, 0]], texcoords=[[0, 0], [1, 0], [1, 1]]
)
mesh.add_triangle(
    [[0, 0, 0], [2, 2, 0], [0, 2, 0]], texcoords=[[0, 0], [1, 1], [0, 1]]
)
texture_file = os.path.join(os.path.dirname(__file__), "cube.png")
mesh.material.texture = texture_file
scene.add_object("mesh", mesh)
scene.run()
        collision.resolve(pair[0], pair[1])
        return True


def hit_aabb(collision, pairs):
    for pair in pairs:
        pair[0].material.color = [0.0, 1.0, 0]
        pair[1].material.color = [0.0, 1.0, 0]
        # Once there is a hit, system will not check
        # for the same collision, if you want to have the objects
        # back in the collision detection pipeline, you have to do
        collision.resolve(pair[0], pair[1])
        return True


scene = Scene(width=600, height=600)
spherical_collision = CollisionTest(callback=hit_sphere,
                                    level=CollisionTest.SPHERICAL)
aabb_collision = CollisionTest(callback=hit_aabb, level=CollisionTest.AABB)

car_object_file = os.path.join(os.path.dirname(__file__), "lib",
                               "Low-Poly-Racing-Car.obj")

spherical_car_1 = Wavefront(filename=car_object_file)
spherical_car_2 = Wavefront(filename=car_object_file)

aabb_car_1 = Wavefront(filename=car_object_file)
aabb_car_2 = Wavefront(filename=car_object_file)

spherical_car_1.position = [-2, 0, 0]
spherical_car_2.position = [-2, 0, 4]
Exemple #4
0
def motion(name, scene, period, total):
    angle = (total * 10) % 360
    px = math.cos(math.radians(angle)) * 8
    py = math.sin(math.radians(angle)) * 8
    scene.objects["nucleus"].children["particle"].position = [px, py, 0]

    sx = math.cos(math.radians(angle * 10)) * 2  # 10 times faster
    sy = math.sin(math.radians(angle * 10)) * 2
    scene.objects["nucleus"].children["particle"].children[
        "sub_particle"
    ].position = [sx, sy, 0]
    scene.lights[0].position = [px, py, 0]
    scene.lights[1].position = [-px, -py, 0]


space = Scene()
space.lights.append(Light())
space.observers[0].position = [20, 20, 20]
space.grid.resize(40, 40, 1)

texture_file = os.path.join(os.path.dirname(__file__), "map.png")

nucleus = Sphere(radius=5, parallels=36, meridians=36)
nucleus.material.texture = texture_file
particle = Sphere()
particle.position = [8, 0, 0]

sub_particle = Sphere(radius=0.5)
sub_particle.position = [0, 2, 0]

nucleus.add_child("particle", particle)
Exemple #5
0
import random
from payton.scene import Scene
from payton.scene.geometry import Sphere
from payton.scene.gui import Hud, Text
from payton.scene.material import YELLOW, RED

sphere_count = 0
score_board = 0
game = Scene()


def create_balloon(name, scene, period, total):
    global sphere_count
    x = random.randint(-5, 5)
    y = random.randint(-5, 5)
    sphere = Sphere()
    sphere.material.color = [1, 1, 1]
    sphere.position = [x, y, 10]
    scene.add_object(f"sphere_{sphere_count}", sphere)
    sphere_count += 1


def move_balloons(name, scene, period, total):
    global sphere_count
    for k in range(sphere_count):
        sphere_name = f"sphere_{k}"
        if sphere_name in scene.objects:
            if not scene.objects[sphere_name].visible:
                continue
            pos = scene.objects[sphere_name].position
            pos[2] -= 0.01
Exemple #6
0
import time

from payton.scene import Scene
from payton.scene.collision import CollisionTest
from payton.scene.geometry import Cylinder

myScene = Scene()
c1 = Cylinder()
c1.position = (3, 3, 0)

c2 = Cylinder()
c2.position = (-3, -3, 0)

c3 = Cylinder()
c3.position = (-3, -3, 3)

moving_part = c2


def makeItRed(collision, pairs):
    global moving_part
    for pair in pairs:
        pair[1].material.color = [1.0, 0, 0]
        collision.resolve(pair[0], pair[1])
    c3.position = [c3.position[0], c3.position[1], c3.position[2] + 0.11]

    moving_part = c3
    myScene.clocks["c2_clocks"].pause()
    print("Collision triggered")

Exemple #7
0
import random
from payton.scene import Scene
from payton.scene.geometry import Cube


def select(list):
    for obj in list:
        obj.material.color = [1, 1, 1]


scene = Scene(on_select=select)
for i in range(10):
    x = random.randint(-5, 5)
    y = random.randint(-5, 5)
    z = random.randint(-5, 5)
    r = random.randint(0, 255) / 255.0
    g = random.randint(0, 255) / 255.0
    b = random.randint(0, 255) / 255.0
    cube = Cube()
    cube.material.color = [r, g, b]
    cube.position = [x, y, z]
    scene.add_object("cube_{}".format(i), cube)

print("Try clicking on objects")

scene.run()