The Flintstone Racing Plan

From GeoMod

Jump to: navigation, search

This is for the car itself, attempt 2


class Stonecar:
    def __init__(self, pos, scale=1):
        self.pos = pos
        self.scale=scale
        self.name="Fred Flintstone"
        self.Stonecar=frame()
        self.lchassis=box(frame=self.Stonecar, pos= (0,0,0),length = 5.25*scale, height = 1*scale, width = 0.25*scale, color=color.magenta)
        self.rchassis=box(frame=self.Stonecar, pos=(0,0,-4*scale),length = 5.25*scale, height = 1*scale, width = 0.25*scale, color=color.magenta)
        self.rearwheel= cylinder(frame=self.Stonecar, pos=(2.5*scale,0,-4*scale), axis=(0,0,4*scale), radius=1*scale,color=(0.8, 0.8, 0.8))
        self.frontwheel=cylinder(frame=self.Stonecar, pos=(-2.5*scale,0,-4*scale), axis=(0,0,4*scale), radius=1*scale,color=(0.8, 0.8, 0.8))
        self.back=box(frame=self.Stonecar, pos=(2.5*scale,2*scale,-2*scale), length=0.5*scale, height = 4*scale, width=3.9*scale)
        self.rightsidesupport=cylinder(frame=self.Stonecar, pos=(2.5*scale,0,-4*scale), axis=(0,4*scale,0), radius=0.1*scale, color=color.magenta)
        self.leftsidesupport=cylinder(frame=self.Stonecar, pos=(2.5*scale,0,0), axis=(0,4*scale,0), radius=0.1*scale, color=color.magenta)
        self.topleftsupport=cylinder(frame=self.Stonecar, pos=(-1.4*scale,4*scale,0), axis=(4*scale,0,0), radius=0.1*scale, color=color.magenta)
        self.toprightsupport=cylinder(frame=self.Stonecar, pos=(-1.4*scale,4*scale,-4*scale), axis=(4*scale,0,0), radius=0.1*scale, color=color.magenta)
        self.roof=box(frame=self.Stonecar, pos=(0.75*scale,4*scale,-2*scale), length=4*scale, height = 0.5*scale, width=3.9*scale)
        self.rearwindow=box(frame=self.Stonecar, pos=(2.5*scale,3*scale,-2*scale), length=.51*scale, height = 1*scale, width=2*scale, color=color.black)
        self.frontsupport=cylinder(frame=self.Stonecar, pos=(-1.3*scale,4*scale,-4*scale), axis=(0,0,4*scale), radius=0.1*scale, color=color.magenta)
        self.stearingcolumn=cylinder(frame=self.Stonecar, pos=(-1.5*scale,0,-1*scale), axis=(.5*scale,.5*scale,0), radius=0.1*scale, color=color.magenta)
        self.stearingwheel=cylinder (frame=self.Stonecar, pos=(-1*scale,.55*scale,-1*scale), axis=(.1*scale,.1*scale,0),radius=.5*scale, color=color.magenta)
        self.seat=box(frame=self.Stonecar, pos=(1*scale,0,-2*scale), length=1*scale, height = 0.125*scale, width=4*scale, color=color.magenta)
        self.seatback=box(frame=self.Stonecar, pos=(1.5*scale,0.5*scale,-2*scale), length=0.2*scale, height =1*scale, width=4*scale, color=color.magenta)
        self.dash=box(frame=self.Stonecar, pos=(-1.5*scale,0.25*scale,-2*scale), length=0.2*scale, height =1*scale, width=4*scale, color=color.magenta)


        for i in scene.objects:
            if i.frame == self.Stonecar:
                i.rotate(angle=pi/2, axis=vector(1,0,0), origin=(0,0,0))
                i.rotate(angle=pi, axis=vector(0,0,1), origin=(0,0,0))
   # def carmov(self): # To move the car
    #    rollangle=pi/4
     #   dist=rollangle/(2*pi)
      #  max_x = 5.
       # min_x = -5.
        #counter=0

        #while 1:
         #   rate(10)
          #  self.rearwheel.rotate(angle=rollangle, axis=self.rearwheel.axis, origin=self.rearwheel.pos)
           # self.frontwheel.rotate(angle=rollangle, axis=self.frontwheel.axis, origin=self.frontwheel.pos)
            #self.Stonecar.pos.x = self.Stonecar.pos.x - dist
            
            #if self.Stonecar.x <= min_x or self.Stonecar.x >= max_x:
             #   rollangle = -rollangle
              #  dist=-dist
               # counter=counter+1
                #print counter
       
    def carmov(self, engine): # To move the car 
        while 1:
            rate(20)
            self.Stonecar.pos.x = (self.racer.pos.x + 1) * engine

    def move(self, new_pos):		#REQUIRED
    	self.pos = new_pos		#REQUIRED
        self.Stonecar.pos = new_pos	#REQUIRED

    def choose_direction(self, local_topo, target_x, target_y, target_dir, dx, max_slope=1): #REQUIRED
        '''local_topo is a 3x3 array of topogarphy data of the area around the car
           target_dir is a vector with the direction of the target relative to the car
              eg: vector(1,0,0) is the x direction (East)
                  vector(0,0,-1) is the negative z direction (North)
                  vector(1,0,-1) is 45 degrees east of due north
           dx is the cell spacing between cells in the 3x3 array
           max_slope is the maximum slope that your car can traverse
           RETURNS a vector indicating which direction you want your car to go'''
        #define directions
        North = vector(0,1,0)
        South = vector(0,-1,0)
        East = vector(1,0,0)
        West = vector(-1,0,0)

        if target_dir.y >= 0.0 and abs(target_dir.y) >= abs(target_dir.x):
            bestway = North
        elif target_dir.y < 0.0 and abs(target_dir.y) >= abs(target_dir.x):
            bestway = South
        elif target_dir.x>=0.0 and abs(target_dir.x) >= abs (target_dir.y):
            bestway = East
        elif target_dir.x<0.0 and abs(target_dir.x) >= abs (target_dir.y):
            bestway = West


        #calculate slope in best direction
        if bestway == North:
            slope = (local_topo[0,1] - local_topo[1,1]) / dx
        elif bestway == South:
            slope = (local_topo[2,1] - local_topo[1,1]) / dx
        elif bestway == East:
            slope = (local_topo [1,2] - local_topo[1,1]) / dx
        elif bestway == West:
            slope = (loca_topo [1,0] - local_topo[1,1]) / dx
        

        if slope < max_slope:
            direction_choice = bestway
        else:
            #second choice
            if bestway == North:
                if target_dir.x >= 0.0:
                    bestway2 = East
                else:
                    bestway2 = West
            if bestway == South:
                if target_dir.x >= 0.0:
                    bestway2 = East
                else:
                    bestway2 = West
            if bestway == East:
                if target_dir.y>= 0.0:
                    bestway2 = North
                else:
                    bestway2=South
            if bestway == West:
                if target_dir.y>=0.0:
                    bestway2=North
                else:
                    bestway2=South
            
            #calculate slope again
            if bestway2 == North:
                slope = (local_topo[0,1] - local_topo[1,1]) / dx
            elif bestway2 == South:
                slope = (local_topo[2,1] - local_topo[1,1]) / dx
            elif bestway2 == East:
                slope = (local_topo [1,2] - local_topo[1,1]) / dx
            elif bestway2 == West:
                slope = (loca_topo [1,0] - local_topo[1,1]) / dx

                if slope < max_slope:
                    direction_choice = bestway2
                else:
                    #3rd choice
                    bestway3 = -bestway2

                    #calculate slope again
                    if bestway3 == North:
                        slope = (local_topo[0,1] - local_topo[1,1]) / dx
                    elif bestway3 == South:
                        slope = (local_topo[2,1] - local_topo[1,1]) / dx
                    elif bestway3 == East:
                        slope = (local_topo [1,2] - local_topo[1,1]) / dx
                    elif bestway3 == West:
                        slope = (loca_topo [1,0] - local_topo[1,1]) / dx


                        if slope < max_slope:
                            direction_choice = bestway3
                        else:
                            #4th choice (Panic)
                            print "call tow truck, this sucks"
                            direction_choice = target_dir


        return direction_choice
        

##    def jump (self, howhigh):
##        self.body.y=self.body.y+howhigh
##        rate (1)
##        self.body.y=self.body.y-howhigh
##
Personal tools