0

With pycharm, I'm using Ursina Engine to make and visualise a simple simulation in 3D with blocks moving about a plane. I have keys set to test the movement of the block and will replace "held_keys" with something else eventually to control the movement of the blocks. My issue is when I press the key for going backwards, the block doesn't go backwards. Also had an issue with time.dt not being identified but in the end, the second import in the code shown worked but I assume I shouldn't have to go through that if I import everything from ursina (*) to begin with...

The block manages to go backwards if I simultaneously hold a "left" or "right" key with the back key, but alone, the cube won't move backwards. I've tried changing the key to make sure it isn't my keyboard. I also have a camera set up to move around in the simulation and the "back" key works for this. I'd appreciate any help or suggestions.

from ursina import *
import direct.showbase.ShowBaseGlobal

if __name__ == '__main__':

    window.vsync = False
    app = Ursina()


    # the ground for the swarm to work from
    ground = Entity(model='plane', scale=(101, 1, 101), color=color.yellow.tint(-.2), texture='white_cube',
                    texture_scale=(100, 100), collider='box')

    target_count = 1
    Target_count = Text(text="Targets Remaining: " + str(target_count), x=-0.87, y=0.48)

    # setting up a timer to record the speed of the units
    start_time = time.time()    # capture the time when the simulation begins
    timer = Text(text="Time: " + str(round(start_time, 3)), x=-0.6, y=0.48)   # print the timer in the top left screen

    def update():
        # updating and printing the timer for use later in the machine learning
        end_time = time.time()  # capture the current time
        time_lapsed = end_time - start_time  # subtract the current time from the start time for the time elapsed
        timer.text = "Time: " + str(round(time_lapsed, 3))  # print the timer

        Target_count.text = "Targets Remaining: " + str(target_count)

    class Unit(Entity):
        def __init__(self, add_to_scene_entities=True, **kwargs):
            super().__init__(add_to_scene_entities, **kwargs)
            self.direction = Vec3(0, 0, 0)

        def update(self):
            dt = direct.showbase.ShowBaseGlobal.globalClock.getDt()
            self.direction = Vec3(
                (self.forward * held_keys['u'] + self.back * held_keys['j'])
                + self.up * (held_keys['7'] - held_keys['8'])
                + self.right * (held_keys['k'] - held_keys['h'])
            ).normalized()  # get the direction we're view to walk in.

            # the ray should start slightly up from the ground, so we can walk up slopes or walk over small objects.
            origin = self.world_position + (
                        self.up * 1)
            hit_info = raycast(origin, self.direction, ignore=(Camera,), distance=.5, debug=False)
            if not hit_info.hit:
                self.position += self.direction * 5 * dt

    class Target(Entity):
        def __init__(self, add_to_scene_entities=True, **kwargs):
            super().__init__(add_to_scene_entities, **kwargs)
            self.touched = 0
# plan to do more with this Target class later


    class Camera(Entity):   # The camera for viewing this project in action
        def __init__(self):
            super().__init__()
            self.height = 0.1
            self.camera_pivot = Entity(parent=self, y=self.height)
            camera.parent = self.camera_pivot
            camera.position = (0, 0, 0)
            camera.rotation = (0, 0, 0)
            camera.fov = 90
            mouse.locked = True
            self.mouse_sensitivity = Vec2(40, 40)
            self.speed = 10
            self.direction = Vec3(0, 0, 0)

        def update(self):
            dt = direct.showbase.ShowBaseGlobal.globalClock.getDt()
            self.rotation_y += mouse.velocity[0] * self.mouse_sensitivity[1]
            self.camera_pivot.rotation_x -= mouse.velocity[1] * self.mouse_sensitivity[0]
            self.camera_pivot.rotation_x = clamp(self.camera_pivot.rotation_x, -90, 90)

            self.direction = Vec3(
                self.forward * (held_keys['w'] - held_keys['s']) * self.speed
                + self.up * (held_keys['q'] - held_keys['e']) * self.speed
                + self.right * (held_keys['d'] - held_keys['a']) * self.speed
            ).normalized()  # get the direction we're view to walk in.

            # the ray should start slightly up from the ground, so we can walk up slopes or walk over small objects.
            origin = self.world_position + (
                     self.up * 0.1)
            hit_info = raycast(origin, self.direction, ignore=(self, Entity), distance=.5, debug=False)
            if not hit_info.hit:
                self.position += self.direction * 5 * dt

    Camera()   # bringing the camera into this simulation

    for i in range(target_count):
        Unit(model='cube', collider='box',  origin_x=i,  origin_y=-.5, origin_z=1, color=color.orange)
        Target(model='cube', collider='box', origin_x=random.randint(-50, 50), origin_y=-0.6,
               origin_z=random.randint(-50, 50), color=color.green)

    app.run()

0 Answers0