joshuaskelly / tmx2map

A tool for converting Tiled tilemaps into Quake maps!
MIT License
16 stars 4 forks source link

Texture rotation incorrect on rotated prefabs #3

Open hemebond opened 4 years ago

hemebond commented 4 years ago

When a prefab is rotated the texture is sometimes incorrect.

E.g., prefab has been rotated 90° counter-clockwise, with door facing west: Before: before

After rotation door faces south and textures not aligned: After: after

Textures on the dominant Z axis (floor/ceiling) seem to be corrected with:

if dominant_axis == Z_AXIS:
    bearing = mathhelper.vector_from_angle(float(q.rotation))
    bearing = tuple(numpy.dot(flip_matrix, (*bearing, 1))[:3])
    q.rotation = mathhelper.angle_between(bearing)

Environment:

python==3.8.2
numpy==1.18.2
six==1.14.0
tmx==1.10
vgio==1.1.2
hemebond commented 4 years ago

I wonder if the rotation direction is wrong. Tiled rotates clockwise by default, whereas Quake/Trenchbroom positive rotation is counter-clockwise.

90° clockwise (270° in Quake) is hflip+dflip. 180° is hflip+vflip. 270° clockwise (90° in Quake) is vflip+dflip.

The issue seems to be on prefabs where hflip is True (90° and 180°cw)

hemebond commented 4 years ago

I think adding this to the end of the plane generation gives the correct result (so far):

# for the Z axis
if dominant_axis == Z_AXIS:
    bearing = mathhelper.vector_from_angle(float(q.rotation))
    bearing = tuple(numpy.dot(flip_matrix, (*bearing, 1))[:3])
    q.rotation = mathhelper.angle_between(bearing)

# for the X and Y axis
if tile.flipped_horizontally:
    if dominant_axis == X_AXIS or dominant_axis == Y_AXIS:
        q.rotation = 360 - q.rotation
        texture_offset = (texture_offset[0] * -1, texture_offset[1])