[pygame] Re : [pygame] car game mechanics

2011-08-31 Thread nathan.o...@gmail.com
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.

[pygame] Re : [pygame] car game AI

2011-09-24 Thread nathan.o...@gmail.com
Yeah,

I read the papers regarding A* realistic movements and the ones concerning the 
steering behaviors. At the moment, i divided my circuit into area and this is 
how i do : i randomly choose one point per area for each ai and i find shortest 
path between those waypoints using A*. This way i have not always the same path 
for all the AI.
So now the idea is to make the movements more realistic. In the paper i read, 
there is something concerning movement on roads ( basically vehicules movement 
i think ) and it said that to provide such kind of movement you should not have 
to smooth the path etc... 
Actually i have my own idea to create kind of more "realistic" movement for my 
ai cars. I think to attach to each car a lure the follow the path and my ai 
only follow the lure. The lure would have a higher speed than the car so it 
should create kind of smooth turns. And in case of collision of the car, i 
thought to just reset the lure position to be in front of the car ànd 
recalculate the path to the next waypoint.

Tell me what you think. Thanks.


- Reply message -
De : "Jake b" 
Pour : 
Objet : [pygame] car game AI
Date : sam., sept. 24, 2011 21:48
Note: Learn how to use vectors. You will need this for steering, and movement. 
(You technically don't, but it's much simpler)

The *very best* tutorial to A* pathfinding : 
http://theory.stanford.edu/~amitp/GameProgramming/ ( Great diagrams )



These are relevant for your racing, and terrain.

Making sure you saw these specifically
http://www.red3d.com/cwr/steer/PathFollow.html
http://www.red3d.com/cwr/steer/Unaligned.html


http://www.red3d.com/cwr/steer/CrowdPath.html
http://www.red3d.com/cwr/papers/1999/gdc99steer.html


http://www.red3d.com/cwr/steer/Obstacle.html
http://www.red3d.com/cwr/steer/Containment.html



Now you could implement physics with pymunk, but that could be overkill at this 
point. If you write your own physics, make sure you keep a constant-time-step 
for stability. If a crash happens, apply a force. And continue steer behavior 
as normal.

[pygame] Re : [pygame] raycasting engine performances (numpy array)

2012-04-27 Thread nathan.o...@gmail.com
In fact it s not for professional stuff or whatever but i am à hge fan of 
python and of old school techniques such as raycasting. I have already wrote a 
wolfenstein clone in c using le libx. Textures and some.effects were 
implemented without any lag ( i can give the code if anyone is interested). So 
now i'd like to write à more advanced wolf/doom like using an OO approach. So i 
though Python of course cause it's soon sexy blablabla but maybe not adapted.
So now my question is, can i write the game logic entirely in python and write 
the rendering part in C with OpenGL calls?
Thank you in advance!

- Reply message -
De : "Weeble" 
Pour : 
Objet : [pygame] raycasting engine performances (numpy array)
Date : ven., avr. 27, 2012 08:36


On Thu, Apr 26, 2012 at 1:49 PM, Nathan Biagini  wrote:
> i dunno if i can do more "optimized" as a drawing line function...

I see a moderate speed-up when replacing:

asf[x][y] = 255

with:

asf[x,y] = 255

Which avoids doing two layers of Python indexing for each pixel.
Still, it doesn't make it fast enough to be smooth. I see a much
bigger speed-up if I replace that loop with:

asf[x,start:end] = 255

This shifts the whole loop into C code with no Python interpreter to
slow it down. The only problem is figuring out how to express the
texturing as something numpy can do do efficiently. I think it may
well be possible, but I'm not enough of a numpy expert to know off the
top of my head.

I totally agree with Greg: only do this if it's for your own education
and amusement. Raycasting on the CPU is always going to be way slower
than a GPU can do, even if you can get the Python interpreter out of
all the slow bits (whether that's by way of numpy, PyPy or just plain
writing some C).