[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re : [pygame] car game mechanics



So i think i can use this tread to ask new question concerning game car development. First of all, since our last discussion i was able to make the car moving  ( thanks to you ) and i added à scrolling process so know i can already drive my little car around the world.

So now it s time for collision part. I managed handling car going out of the road by using pygame.mask module. It works pretty well. However i would like to dispatch fix obstacles. To manage this i thought to use a second mask but it s à bit tricky since i only have.the center position of the car ( regarding the code i joined ) but i want to check if the 4 points/pixels representing the 4 edges of the car. So i ll need to determinate those points with the center pos? And what about the rotation? So i think i could do it but i want to know how you would solve this? Is there à better way to do? Simpler? Assuming my map is just à simple bmp, my idea was to deal with collision only with mask.

Thanks.

----- Reply message -----
De : "Joe Ranalli" <jranalli@xxxxxxxxx>
PourÂ: <pygame-users@xxxxxxxx>
Objet : [pygame] car game mechanics
Date : mar., aoÃt 30, 2011 16:33


Just to add one point, the math Nathan is concerned about is technically sound for basic sprite behavior. The issue causing dramatically unrealistic behavior is something caused by the implementation of it. Specifically that numerically, some of the values are being rounded to the nearest integer, which you don't want to happen for realistic movement.

The article that Nathan linked does provide a method for more complex treatment of a car. Instead of treating the car as simply rotating about its center, they're accounting for the fact that a real car steers using its front wheels while the back wheels are fixed. Even implementing that approach in python using Nathans current programming method would cause unrealistic behavior due to similar rounding and transformation size errors .

On Tue, Aug 30, 2011 at 10:22 AM, Christopher Night <cosmologicon@xxxxxxxxx> wrote:
Three things you can do to make this more realistic.

1. use self.angle as the argument to pygame.transform.rotate instead of a_deg, as Joe suggested.

2. use separate variables to keep the car's position. Don't store it in self.rect.center. This is because rect positions are coerced to ints, so if your car should be moving 0.5 pixels per frame to the right, this will be rounded down to 0.

3. update self.rect after you transform. This is because the rotated image doesn't have the same size as the original image, so its center will be offset. Also, your second argument to screen.blit should be self.rect, not self.rect.center. (If you pass it a position like self.rect.center, it will use that as the upper-left position of the blitted rect.

I modified your file to have this in your init method:

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.x, self.y = SCREEN_SIZE_X/2, SCREEN_SIZE_Y - self.rect.height * 2


this in your update:

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ #rotate the car
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.sprite = pygame.transform.rotate(self.original, self.angle * -1)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.rect = self.sprite.get_rect(center = self.rect.center)
Â
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ #move the car
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.x += speedx
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.y += speedy
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.rect = self.sprite.get_rect()
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ self.rect.center = self.x, self.y


and this in your draw:

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ screen.blit(self.sprite, self.rect)


And it looked much better to me. Let me know what you think.

-Christopher


On Tue, Aug 30, 2011 at 5:46 AM, Nathan BIAGINI <nathan.open@xxxxxxxxx> wrote:
Hi,

i'm trying to write a car game and i have decided to start by the movement part. I calculate the X and Y components of the car and update his position by increasing the current one with the two new components. The two components depend of the orientation of the car, managed by the right and left arrow :


speedx = math.sin(self.angle * (math.pi/180)) * SPEED
speedy = math.cos(self.angle * (math.pi/180)) * SPEED * -1

Where SPEED is a constant and self.angle a int representing the orientation. I don't know if this is the best way because, the car speed depends of his orientation. Increasing the self.angle value will increase the components, this is not suitable for a car game i think.

I have another problem with the car movement. Each time the orientation change, i would like to rotate the car in the right direction. I thought to do something like that :

a_rad = math.asin(speedx/SPEED)
a_deg = math.degrees(a_rad) Â
self.sprite = pygame.transform.rotate(self.original, a_deg * -1)
self.rect = self.sprite.get_rect(center = self.rect.center)

It does not work properly, this way, my car can't rotate over 90Â because of the speedx values.

Here is my code :

http://pastebin.com/VJgQRtYq

and the sprite i use for the car is joined.

Thanks for reading me.