Re: [pygame] car game mechanics
On Tue, Aug 30, 2011 at 10:33 AM, Joe Ranalli wrote: > 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 > . I read that article and I think I found a problem with the algorithm it presents. If you try to implement that algorithm and it gives you bad results, check out the comment that I left there. -Christopher
[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" Pour : 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 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 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.
Re: [pygame] car game mechanics
Oops. I realize I missed the line where you are updating self.rect after calling transform. I shouldn't have put it in there again. The bit I posted from the update method should only have read: #move the car self.x += speedx self.y += speedy #rotate the car self.sprite = pygame.transform.rotate(self.original, self.angle * -1) self.rect = self.sprite.get_rect(center = (self.x, self.y)) Sorry for the confusion! -Christopher On Tue, Aug 30, 2011 at 10:22 AM, Christopher Night 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 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. >> > >
Re: [pygame] car game mechanics
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 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 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. >> > >
Re: [pygame] car game mechanics
Yeah actually this is great! Thanks all for your help. I'll try go ahead and go to the next step. :) 2011/8/30 Christopher Night > 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 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. >> > >
Re: [pygame] car game mechanics
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 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. >
Re: [pygame] car game mechanics
Yeah indeed it works for the rotation but the movement are not realist at all the way i did. I found this on the internet : http://engineeringdotnet.blogspot.com/2010/04/simple-2d-car-physics-in-games.html it's illustrated by C++ example so i would like to know if there is an equivalent class to the Vector2 C++ class? Or maybe can i just a numpy array instead? 2011/8/30 Joe Ranalli > You already have self.angle. Why not just do your transform as a function > of that? > > a_deg = self.angle % 360 > > sprite = pygame.transform.rotate(self.original, a_deg * -1) > > On Tue, Aug 30, 2011 at 7:21 AM, Nathan BIAGINI wrote: > >> Doing something like : >> >> a_rad = math.atan2(speedy, speedx) >> >> ? >> >> I can turn 360 degrees yeah but it does not make proper rotation... i ll >> try to find something on the internet about 2d car physics. >> >> >> 2011/8/30 Weeble >> >>> >>> On Aug 30, 2011 10:46 AM, "Nathan BIAGINI" >>> wrote: >>> >>> >> a_rad = math.asin(speedx/SPEED) >>> >>> Try math.atan2, feeding it both the x and y components of the velocity. >>> >> >> >
Re: [pygame] car game mechanics
You already have self.angle. Why not just do your transform as a function of that? a_deg = self.angle % 360 sprite = pygame.transform.rotate(self.original, a_deg * -1) On Tue, Aug 30, 2011 at 7:21 AM, Nathan BIAGINI wrote: > Doing something like : > > a_rad = math.atan2(speedy, speedx) > > ? > > I can turn 360 degrees yeah but it does not make proper rotation... i ll > try to find something on the internet about 2d car physics. > > > 2011/8/30 Weeble > >> >> On Aug 30, 2011 10:46 AM, "Nathan BIAGINI" wrote: >> >> >> a_rad = math.asin(speedx/SPEED) >> >> Try math.atan2, feeding it both the x and y components of the velocity. >> > >
Re: [pygame] car game mechanics
Doing something like : a_rad = math.atan2(speedy, speedx) ? I can turn 360 degrees yeah but it does not make proper rotation... i ll try to find something on the internet about 2d car physics. 2011/8/30 Weeble > > On Aug 30, 2011 10:46 AM, "Nathan BIAGINI" wrote: > > >> a_rad = math.asin(speedx/SPEED) > > Try math.atan2, feeding it both the x and y components of the velocity. >
Re: [pygame] car game mechanics
On Aug 30, 2011 10:46 AM, "Nathan BIAGINI" wrote: >> a_rad = math.asin(speedx/SPEED) Try math.atan2, feeding it both the x and y components of the velocity.
[pygame] car game mechanics
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. <>