Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Python - Tron Demo

Using Qt Quick and the Qt 3D QML API

bezier_curves = [
    [(318, 84), (479, 18), (470, 233), (472, 257)],
    [(472, 257), (473, 272), (494, 459), (419, 426)],
    [(419, 426), (397, 417), (354, 390), (324, 396)],
    [(324, 396), (309, 399), (217, 416), (202, 415)],
    [(202, 415), (157, 412), (116, 278), (114, 263)],
    [(114, 263), (119, 219), (151, 190), (182, 192)],
    [(182, 192), (277, 192), (216, 128), (318, 84)]
]
road_start_position = self.m_points_at_position[0]
screen_origin_position = self.m_points_at_position[50]
def compute_angle_between_road_section_and_z():
    # We want to look toward -Z
    target_dir = QVector3D(0.0, 0.0, -1.0)
    # Our current dir
    current_dir = (screen_origin_position - road_start_position).normalized()
    # Angle between our two vectors is acos(dot, current_dir, target_dir)
    dot = QVector3D.dotProduct(target_dir, current_dir)
    return acos(dot)
rot_angle = compute_angle_between_road_section_and_z()
self.m_road_to_world_matrix = QMatrix4x4()
# Rotate of rot_angle around +Y
self.m_road_to_world_matrix.rotate(degrees(rot_angle), QVector3D(0.0, 1.0, 0.0))
# Translate points back to origin
self.m_road_to_world_matrix.translate(-screen_origin_position)
from PySide2.QtCore import Property, Signal, QByteArray
from PySide2.Qt3DCore import Qt3DCore
from PySide2.Qt3DRender import Qt3DRender
from array import array
class RoadLineGeometry(Qt3DRender.QGeometry):
    def __init__(self, parent=None):
        Qt3DRender.QGeometry.__init__(self, parent)
        self.m_position_buffer = Qt3DRender.QBuffer(self)
        self.m_position_buffer.setUsage(Qt3DRender.QBuffer.StaticDraw)
        self.m_position_attribute = Qt3DRender.QAttribute(self)
        self.m_position_attribute.setAttributeType(Qt3DRender.QAttribute.VertexAttribute)
        self.m_position_attribute.setDataType(Qt3DRender.QAttribute.Float)
        self.m_position_attribute.setDataSize(3)
        self.m_position_attribute.setName(Qt3DRender.QAttribute.defaultPositionAttributeName())
        self.m_position_attribute.setBuffer(self.m_position_buffer)
        self.addAttribute(self.m_position_attribute)
    def update(self, data, width):
        # Data is a QByteArray of floats as vec3
        float_data = array('f', data.data())
        transformed_point = [v for i in range(0, len(float_data), 3)
                             for v in [float_data[i] - width / 2.0, 0.0, float_data[i + 2],
                                       float_data[i] + width / 2.0, 0.0, float_data[i + 2]]]
        self.m_position_buffer.setData(QByteArray(array('f', transformed_point).tobytes()))
        self.m_position_attribute.setCount(len(transformed_point) / 3)

6 Comments

26 - Jul - 2018

GIto

27 - Jul - 2018

David Murphy

27 - Jul - 2018

Paul Lemire

23 - Mar - 2021

Nigel Brown

19 - May - 2021

Paul Lemire

1 - Jan - 2024

Juliusz Kaczmarek

PaulLemire

Paul Lemire

Senior Software Engineer