Skip to content

Commit a8080b6

Browse files
Try fix calcs after rework
1 parent 798f0ea commit a8080b6

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

loco-graphics-helper/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def prepare_scene(self):
9999
for c in self.target_object.children:
100100
c.hide_render = False
101101

102-
object.location = self.target_object.location
102+
object.location = self.target_object.matrix_world.translation
103103

104104
# This is a little hacky...
105105
if self.layer == 'Top Down Shadow':

loco-graphics-helper/rct_graphics_helper_panel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ def draw_vehicle_panel(self, scene, layout):
248248
anim_location = 0
249249
front_name = '' if front is None else front.name
250250
back_name = '' if back is None else back.name
251+
mid_point_x = component.get_preferred_body_midpoint()
252+
if not math.isclose(body.matrix_world.translation[0], mid_point_x, rel_tol=1e-4):
253+
warning = "BODY LOCATION IS NOT AT PREFERRED MID X POINT! {}".format(mid_point_x)
254+
251255
if not front is None:
252256
front_position = component.get_bogie_position(SubComponent.FRONT)
253257
back_position = component.get_bogie_position(SubComponent.BACK)
@@ -259,9 +263,7 @@ def draw_vehicle_panel(self, scene, layout):
259263
if component.get_number_of_sprites(SubComponent.BACK) != 0:
260264
back_idx = back.loco_graphics_helper_vehicle_properties.index - 1
261265
back_idx = back_idx + 180 if front.loco_graphics_helper_vehicle_properties.is_inverted else back_idx
262-
mid_point_x = (front.location[0] - back.location[0]) / 2 + back.location[0]
263-
if not math.isclose(body.location[0], mid_point_x, rel_tol=1e-4):
264-
warning = "BODY LOCATION IS NOT AT MID X POINT BETWEEN BOGIES! {}".format(mid_point_x)
266+
265267
anim_location = component.get_animation_location()
266268
if anim_location > 255 or anim_location < 0:
267269
warning = "Animation is too far from bogies"

loco-graphics-helper/vehicle.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ def get_object(self, sub_component: SubComponent):
3535
SubComponent.BODY.value:self.body,
3636
}
3737
return object_mapping[sub_component.value]
38+
39+
40+
def has_sprites(self, sub_component: SubComponent):
41+
object = self.get_object(sub_component)
42+
if object is None:
43+
return False
44+
props = object.loco_graphics_helper_vehicle_properties
45+
if props.is_clone:
46+
return False
47+
48+
if all(v == 0 for v in props.sprite_track_flags):
49+
return False
50+
51+
return True
3852

3953
def get_number_of_sprites(self, sub_component: SubComponent):
4054
object = self.get_object(sub_component)
@@ -64,16 +78,22 @@ def get_number_of_sprites(self, sub_component: SubComponent):
6478
return int(num_sprites)
6579

6680
def _get_min_max_x_bound_box_corners_with_children(self, object):
81+
return self._get_min_max_axis_bound_box_corners_with_children(object, 0)
82+
83+
def _get_min_max_z_bound_box_corners_with_children(self, object):
84+
return self._get_min_max_axis_bound_box_corners_with_children(object, 2)
85+
86+
def _get_min_max_axis_bound_box_corners_with_children(self, object, axis):
6787
mins = []
6888
maxs = []
69-
min_x, max_x = self._get_min_max_x_bound_box_corners(object)
89+
min_x, max_x = self._get_min_max_axis_bound_box_corners(object, axis)
7090
# This can happen if there are no dimensions to this object (or if its 0 width)
7191
if min_x != max_x:
7292
mins.append(min_x)
7393
maxs.append(max_x)
7494

7595
for c in object.children:
76-
min_x, max_x = self._get_min_max_x_bound_box_corners_with_children(c)
96+
min_x, max_x = self._get_min_max_axis_bound_box_corners_with_children(c, axis)
7797
if min_x != max_x:
7898
mins.append(min_x)
7999
maxs.append(max_x)
@@ -82,10 +102,10 @@ def _get_min_max_x_bound_box_corners_with_children(self, object):
82102

83103
return (min(mins), max(maxs))
84104

85-
def _get_min_max_x_bound_box_corners(self, object):
105+
def _get_min_max_axis_bound_box_corners(self, object, axis):
86106
bbox_corners = [object.matrix_world * Vector(corner) for corner in object.bound_box]
87-
min_x = min([x[0] for x in bbox_corners])
88-
max_x = max([x[0] for x in bbox_corners])
107+
min_x = min([x[axis] for x in bbox_corners])
108+
max_x = max([x[axis] for x in bbox_corners])
89109
return (min_x, max_x)
90110

91111
def get_half_width(self):
@@ -110,6 +130,16 @@ def get_half_width(self):
110130
max_x = max(maxs) - self.body.matrix_world.translation[0]
111131

112132
return max(min_x, max_x)
133+
134+
def get_preferred_body_midpoint(self):
135+
if self.has_sprites(SubComponent.FRONT) or self.has_sprites(SubComponent.BACK):
136+
# If it has a real bogey we should put midpoint between the two bogies
137+
mid_point_x = (self.front.location[0] - self.back.location[0]) / 2 + self.back.location[0]
138+
else:
139+
# If it has fake/no bogies we should put midpoint in the centre of the body
140+
body_min_x, body_max_x = self._get_min_max_x_bound_box_corners_with_children(self.body)
141+
mid_point_x = (body_min_x + body_max_x) / 2
142+
return mid_point_x
113143

114144
def get_bogie_position(self, sub_component: SubComponent):
115145
assert sub_component != SubComponent.BODY
@@ -123,10 +153,11 @@ def get_animation_location(self):
123153
if len(self.animations) == 0:
124154
return 0
125155
x_diff = self.front.location[0] - self.back.location[0]
156+
print("front_x {} back_x {} anim_x {}".format(self.front.location[0], self.back.location[0], self.animations[0].location[0]))
126157
x_factor = (1 / x_diff)
127158
anim_diff = self.front.location[0] - self.animations[0].location[0]
128159
anim_factor = (anim_diff * x_factor) * 128
129-
return int(anim_factor) + 128
160+
return int(anim_factor) + 64
130161

131162

132163
def get_car_components(cars) -> List[VehicleComponent]:

0 commit comments

Comments
 (0)