on as grosièrement le déplacement selon la route

This commit is contained in:
leo 2022-02-07 17:55:12 +01:00
parent a6339aecba
commit bde77c760d
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
4 changed files with 41 additions and 10 deletions

32
Car.py
View File

@ -1,5 +1,6 @@
import sumolib import sumolib
import pygame as pg import pygame as pg
import math
class Car: class Car:
def initPath(self): def initPath(self):
@ -12,26 +13,47 @@ class Car:
self.laneShape=startEdge.getLane(laneId).getShape() self.laneShape=startEdge.getLane(laneId).getShape()
if(inverted): if(inverted):
self.laneShape.reverse() self.laneShape.reverse()
def __init__(self,carID,route,parentMap,surface): def __init__(self,carID,route,parentMap,surface):
self.id=carID self.id=carID
self.route=route self.route=route
self.map=parentMap self.map=parentMap
self.index=0 self.index=0
self.laneInd=0
startEdge=self.map.getEdge(route[0]) startEdge=self.map.getEdge(route[0])
nextEdge=self.map.getEdge(route[1]) nextEdge=self.map.getEdge(route[1])
self.initPath() self.initPath()
self.pos=self.laneShape[0] self.pos=list(self.laneShape[0])
self.v=50
self.a=0
self.surf=surface self.surf=surface
def draw(self): def draw(self,col):
pg.draw.circle(self.surf,(255,0,0),self.map.convertPos(self.pos),5) pg.draw.circle(self.surf,col,self.map.convertPos(self.pos),5)
def update(self,dt): def update(self,dt):
lgt=self.v*dt lgt=self.v*dt
while(lgt>0): while(lgt>0):
pass endPos=self.laneShape[self.laneInd+1]
l=math.dist(self.pos,self.laneShape[self.laneInd+1])
if lgt>=l:
lgt-=l
pos=list(self.laneShape[-1])
self.laneInd+=1
if(self.laneInd>=len(self.laneShape)-2):
self.laneInd=0
self.index+=1
if(self.index>=len(self.route)-2):
self.index=0
self.initPath()
self.pos=list(self.laneShape[self.laneInd])
continue
adv=lgt/l
self.pos[0]+=(endPos[0]-self.pos[0])*adv
self.pos[1]+=(endPos[1]-self.pos[1])*adv
lgt=0

View File

@ -1,5 +1,6 @@
import sumolib import sumolib
from Car import Car from Car import Car
from matplotlib import cm
class CarController: class CarController:
def __init__(self,path,parentMap,surface): def __init__(self,path,parentMap,surface):
@ -12,7 +13,13 @@ class CarController:
for vehicle in sumolib.xml.parse(path,"vehicle"): for vehicle in sumolib.xml.parse(path,"vehicle"):
self.cars.append(Car(vehicle.id,vehicle.route[0].edges.split(),self.map,self.surf)) self.cars.append(Car(vehicle.id,vehicle.route[0].edges.split(),self.map,self.surf))
def draw(self,screen): def update(self):
for car in self.cars: for car in self.cars:
car.draw() car.update(1.0/60)
def draw(self,screen):
cmap = cm.get_cmap('Spectral')
for ind,car in enumerate(self.cars):
color=cmap(ind/len(self.cars))
car.draw([a*255 for a in color[0:3]])
screen.blit(self.surf,(0,0)) screen.blit(self.surf,(0,0))

4
Map.py
View File

@ -6,11 +6,11 @@ import sumolib
import pygame as pg import pygame as pg
class Map: class Map:
def __init__(self,path : str,surface : pg.Surface): def __init__(self,path,surface):
self.fromPath(path) self.fromPath(path)
self.surf=surface self.surf=surface
def fromPath(self,path : str): def fromPath(self,path):
self.net = sumolib.net.readNet(path,withInternal=True) self.net = sumolib.net.readNet(path,withInternal=True)
def draw(self,screen): def draw(self,screen):

View File

@ -36,8 +36,10 @@ while running:
running = False running = False
elif event.type == pg.KEYDOWN and event.key == pg.K_q: elif event.type == pg.KEYDOWN and event.key == pg.K_q:
running = False running = False
carSurf.fill((0,0,0))
m.draw(screen) m.draw(screen)
cc.update()
cc.draw(screen) cc.draw(screen)
pg.display.flip() pg.display.flip()