I'm writing a player class that, among other things, has mesh attributes (I use the py3d library and the mesh class from it) and collider (a class that I need to implement myself). The collider is a simple cube and should have a method to determine whether it collided with another collider-cube or not. I have a class that allows you to rotate and move 3d objects, I inherit the collider from it. The main problem is precisely to write a collision check function
I tried to use the methods built into Pygame to detect collisions, but it didn't work, because when the camera is removed, the collider remains the same size, and it can't be rotated. I'm bad at math, and all the guides I found were in C.game example
One way to detect box-box collisions in 3D using PyOpenGL and Pygame is to use the Bullet physics engine. Bullet is a 3D physics engine that can be used to detect collisions, apply forces, and simulate the motion of rigid bodies. To use Bullet, you would need to implement the collider class as a Bullet body, and then use the Bullet functions to detect collisions between the collider objects. You can also use the Bullet functions to rotate and move the colliders, which will allow you to keep the same size collider regardless of the camera position.
Here's a link to a tutorial on how to integrate bullet
http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-a-physics-library/
Related
I am making a game in python with pygame, and I am wondering if there is any class (with collisions) in pygame that supports non-AABB polygons. I know there is the pygame.draw.polygon function, but that takes a list of points instead of a rect like pygame.draw.rect.
Is there any way to do this in pygame?
If not, are there any pygame-compatible libraries that could do this?
If there are no libraries, how could I implement it?
The pygame.draw module does not generate objects. It just draws something on a Surface. You cannot use this for collision detection. I think you're confusing pygame.Rect and the pygame.draw module.
The collision detection in pygame is based on rectangles or circles. See How do I detect collision in pygame?.
Another option is to use pygame.mask.Mask objects, which you can use to detect overlapping shapes and sprites. See Pygame mask collision
Can anyone tell me the use of Pygame.Rect. I am creating a simple program to draw and display rectangle in screen and check for collision with screen. I saw a video on youtube where the Youtuber uses the following code:
moving_rect=pygame.Rect(300,300,100,100)
Now I don't know what is Pygame.Rect is used for and what the above line of code actually does.
A pygame.Rec describes a rectangular area. It can be use to define the bounding box of an object. This can be useful for a collision test. See How do I detect collision in pygame?.
Actually it is used to define the location of a pygame.sprite.Sprite object.
Notice that a pygame.Surface object has no position. It only contains the pixel information. However, a Rect and a Surface together define the position of an image (Sprite) in the window.
What is the difference between importing a rocket sprite (for example) and setting it as an image vs importing a rocket sprite and setting it as a sprite in pygame?
I think you're just getting confused about the terminology:
Image
An image is just a collection of pixels. You're using "sprite" to refer to an image on the disk, but that's just an image file. To use your rocket example, you would load the image like this:
rocket_img = pygame.image.load('rocket.png').convert_alpha()
You can then draw this image anywhere you want with:
screen.blit(rocket_img, (x, y))
Sprite
A sprite in Pygame is an object, with a whole collection of built-in functionality. Sprites have an image as one of their properties, but there are a whole lot more. Plus you can put sprites together in groups to make them easier to update or draw. Sprites have collision functionality built into them. You can add your own properties to track location, velocity, animation, etc.
A simple sprite:
class Rocket(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('rocket.png').convert_alpha()
self.rect = self.image.get_rect()
def update(self):
self.rect.x += 1
This would be a rocket sprite that you would instantiate by using
rocket = Rocket()
You can draw by using
screen.blit(rocket.image, rocket.rect)
and it moves slowly to the right (if you call update() in the game loop:
rocket.update()
I recommend looking at the Sprite docs - there's lots more you can do with groups to make working with lots of sprites very easy.
http://www.pygame.org/docs/ref/sprite.html
In Pygame "images" generally refer only to image files: importing and exporting them to disk. There is the "Surface" object that is a Python object that holds pixels, and can be used to stamp other surfaces, be transformed (scaled/rotated) , yielding other surfaces and so on.
The main screen itself is a Surface subclass - so when you stamp a Surface with data read from a disk image, using the blit method the image shows up on the screen.
Sprites on the other hand are a base class for objects in your game, and they don't even depend of having attached pixels data with them. Some of the Pygame API expect Sprite objects to have a rect attribute, which denotes the position where it will be rendered on a Surface - and an image attribute. If it is to be used, the sprite.image attribute should hold a surface object - usually read from disk (but could have been programatically drawn).
The main call using the sprite image attribute is the Group.draw() method.
But it is possible to create an entirely different game than an interactive one - one that would be the server side for a MMO game, without anything on the screen, for example, using the Sprite and Group classes, without ever making use of the image attribute on sprites.
Worth reading:
https://www.pygame.org/docs/ref/sprite.html
Conversely, you can bypass all the helper logic provided by Sprites and Groups and create a game that will only ever have Surface objects - representing images read from disk. Them you are responsible to track were and when to draw them, without using the elpe rmethods in sprite Groups.
I'm a student in Computer Programming at my high school and really need help to make the square an obstacle that ships can't enter and bullets disappear once you shoot at the square also for the circle the same thing. And both ships die if they collide with each other. Please help I know my question isn't the best worded but i really need help.
the link to the code is here https://docs.google.com/file/d/0B-Pb_T-Vgr3-TnhRY0lvVjJHX0U/edit?usp=sharing
I would suggest that you create a GameObject class which extends pygame.sprite.Sprite and both Ship and Bullet classes which extend GameObject. This allows you to then easily add properties that both will need, such as velocity and acceleration, and you can create a collide method that is overridden by both for specific behavior. The added bonus here is that pygame.sprite.collide_rect will work properly as such:
if (pygame.sprite.collide_rect(sprite1, sprite2)):
# sprite1 and sprite2 are colliding!
# do something, such as calling sprite1.collide(sprite2)
# and sprite2.collide(sprite1)
So, pygame.sprite.collide_rect checks if two sprites collide using the Sprite.rect property of each. This functionality could also be recapitulated using rect.colliderect:
sprite1.rect.colliderect(sprite2.rect)
How do I tell python to detect whether two objects/ images touch each other? For example when an image of pacman touches an image of a ghost?
http://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect
colliderect()
test if two rectangles overlap
colliderect(Rect) -> bool
Returns true if any portion of either rectangle overlap (except the
top+bottom or left+right edges).
If the only collision detection between sprites is between pac-man and other objects, then just call colliderect on every combination of pacman's collision rectangle and every other collision rectangle.
If every single combination of collisions can be meaningful, then produce a big list of all of them and colliderect each rectangle with each rectangle further along in the list.
Every collision that occurs, you can choose to do something - you could even call both objects, passing the other object that collided, and thereby allowing the logic to be contained within one or both of the objects.
I'm assuming you're using Sprites for your pacman and ghost? If so you want one of the sprite collision functions: http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide
Otherwise, use the Rect collision Patashu links.