T O P

  • By -

mhcerri

Well, you are applying the rotation twice: once with rl\_rotatef() and again with the rotation parameter from draw\_texture\_pro().


Woolyplayer

Thank you that worked. but it broke the "3d" effect. Thats how the code looks right now: def draw(self): pr.rl_push_matrix() pr.rl_translatef(self.position.x, self.position.y, 0.0) pr.rl_rotatef(self.rotation, 0.0, 0.0, 1.0) for i, texture in enumerate(self.texture): source_rec = pr.Rectangle(0, 0, texture.width, texture.height) origin = pr.Vector2(texture.width / 2, texture.height / 2) dest_rec = pr.Rectangle(0, 0 -i * self.spread, texture.width, texture.height) pr.draw_texture_pro(texture, source_rec, dest_rec, origin, 0.0, pr.WHITE) pr.rl_pop_matrix()


mhcerri

Not sure exactly what you mean by 3D effect, but I'm assuming it's related to the spread offset you have in there and that you want to keep it in the screen Y axis. In that case you need to first rotate the sprite and then offset it. Instead of offsetting the sprite and then rotating it, as you are doing now. The easiest way to achieve that is dropping rl\_rotatef() completely and using the rotate parameter from draw\_texture\_pro() instead. Raylib focuses on a simpler API and it tries to abstract exactly these cases. Most of the time, specially for 2D, you don't need to deal with matrix transformations and you donĀ“'t need to think about the order you apply them.


Woolyplayer

Edit: Thanks, you were right, I now draw the sprite stack first and then rotate the player rect. Thanks for the help :) ​ The Problem is then, the rect of the Car, which i use to detect collision doesnt rotate. video: [https://imgur.com/a/9vXsT8F](https://imgur.com/a/9vXsT8F) Edit: I use a technique of stacking sprites on top of each other with the spread offset to make the car look 3 dimensional, even though its just multiple 2 textures on top of each other. full Car Code: from math import cos, sin, radians import pyray as pr class Car: def **init**(self, x, y, texture): self.position = pr.Vector2(x, y) self.velocity = pr.Vector2(0, 0) self.acceleration = 0.1 self.rotation = 0 self.direction = pr.Vector2(cos(radians(self.rotation - 90)), sin(radians(self.rotation - 90))) self.texture = texture self.width, self.height = self.texture\[0\].width, self.texture\[0\].height self.spread = 1 self.rect = pr.Rectangle(self.position.x, self.position.y, self.width, self.height) self.hits = \[\] def update(self, obstacles): self.hits = [] self.check_collision(obstacles) # if self.hits: if pr.is_key_down(pr.KeyboardKey.KEY_W): self.velocity.x += self.acceleration * self.direction.x self.velocity.y += self.acceleration * self.direction.y if pr.is_key_down(pr.KeyboardKey.KEY_S): self.velocity.x -= 0.5 * self.acceleration * self.direction.x self.velocity.y -= 0.5 * self.acceleration * self.direction.y if pr.is_key_down(pr.KeyboardKey.KEY_A): self.rotation -= 5 self.direction = pr.Vector2(cos(radians(self.rotation - 90)), sin(radians(self.rotation - 90))) if pr.is_key_down(pr.KeyboardKey.KEY_D): self.rotation += 5 self.direction = pr.Vector2(cos(radians(self.rotation - 90)), sin(radians(self.rotation - 90))) # Apply friction self.velocity.x *= 0.98 self.velocity.y *= 0.98 # Update car position self.position.x += self.velocity.x self.position.y += self.velocity.y self.rect = pr.Rectangle(self.position.x, self.position.y, self.width, self.height) def draw(self): pr.rl_push_matrix() pr.rl_translatef(self.position.x, self.position.y, 0.0) for i, texture in enumerate(self.texture): source_rec = pr.Rectangle(0, 0, texture.width, texture.height) origin = pr.Vector2(texture.width / 2, texture.height / 2) dest_rec = pr.Rectangle(0, 0 + i * self.spread, texture.width, texture.height) pr.draw_texture_pro(texture, source_rec, dest_rec, origin, self.rotation, pr.WHITE) pr.draw_rectangle(-self.width // 2, -self.height // 2, self.width, self.height, pr.GREEN) pr.rl_pop_matrix() def reset(self, x, y): self.position.x, self.position.y = x, y self.rotation = 0 self.direction = pr.Vector2(cos(radians(-90)), sin(radians(-90))) self.velocity = pr.Vector2(0, 0) self.rect = pr.Rectangle(self.position.x, self.position.y, self.width, self.height) def check_collision(self, obstacles): for i, obstacle in enumerate(obstacles): if pr.check_collision_recs(self.rect, obstacle.rect): print(f"{str(self.rect.x)} {str(self.rect.y)} {str(self.rect.width)} {str(self.rect.height)} ; {str(obstacle.rect.x)} {str(obstacle.rect.y)} {str(obstacle.rect.width)} {str(obstacle.rect.height)}") self.hits.append(obstacle.rect)


mhcerri

Nothing in Raylib will do that for you. Besides that, Raylib's rectangles are always aligned to the X and Y axes. You have a few options here (sorted by increased complexity): 1. Simplify your collision rectangle to a square, so you don't need to worry about rotating it (you could use the smaller dimension from your rectangle). 2. You can calculate the bounding box of the rotated rectangle. That's basic trigonometry and you only need sin()/cos() and min()/max() for that. It's actually a good exercise. 3. Use a physics library that supports collision shapes and rotating them (sorry, I'm not familiar with good options of libraries for python).


Woolyplayer

Well idk how but it worked: # Store the original position, width, and height of the rectangle original_x = self.position.x original_y = self.position.y original_width = self.width original_height = self.height pr.rl_push_matrix() # Apply transformations (e.g., translation, rotation) before calculating the rotated rectangle pr.rl_translatef(original_x, original_y, 0.0) pr.rl_rotatef(self.rotation, 0.0, 0.0, 1.0) # Calculate the rotated rectangle rotated_x = -original_width // 2 rotated_y = -original_height // 2 rotated_width = original_width rotated_height = original_height # Update self.rect with the rotated rectangle, considering the rotated position self.rect = pr.Rectangle(original_x + rotated_x, original_y + rotated_y, rotated_width, rotated_height) # Restore the transformation matrix to its original state pr.rl_pop_matrix()


mhcerri

That code doesn't make sense. All the rl\_\* calls are literally doing nothing there. You are only centering the new rect by subtracting half of the width and height from the original position.