def calc(self, player): if self == player or not ac.isConnected(self.id) or ac.isCarInPitline( self.id): self.isVisible = False return # we are visible, so it's worth calculating the car's properties self.isVisible = True x, y, z = self.calcCar() # and add the stuff relative to the driver self.relativePositionMeters = euclid.Point2( x - player.currentWorldPosition.x, z - player.currentWorldPosition.y) self.playerDistance = player.currentWorldPosition.distance( euclid.Point2(x, z))
def point_to_world( self, p ): '''returns an euclid.Vector2 converted to world space :rtype: euclid.Vector2 ''' v = euclid.Point2( p[0], p[1] ) matrix = self.get_world_transform() return matrix * v
def point_to_local( self, p ): '''returns an euclid.Vector2 converted to local space :rtype: euclid.Vector2 ''' v = euclid.Point2( p[0], p[1] ) matrix = self.get_world_inverse() return matrix * v
def Transform(Points, Aff): import euclid if Aff is None: return Points TPoints = [] for P in Points: P = Aff * euclid.Point2(*P) TPoints.append([P[0], P[1]]) return TPoints
def __init__(self, pos=euclid.Vector3(0,0,0)): super(ZoneView,self).__init__(pos,reverse_draw=True) self._pos.set_transition(dt=0.001) self.selected_width = anim.animate(0, 0, dt=0.3, method="sine") self.sorted = False self.padding = 15 points = [(0.0, 0.0), (26.0, 244.0), (184.0, 368.0), (400.0, 226.0)] self.path = BezierPath(*[euclid.Point2(v[0], v[1]) for v in points]) self.visible = anim.animate(0,0,dt=0.3) self.layout = self.layout_straight self.is_library = False
def test_add(self): a = (3.0, 7.0) b = (1.0, 2.0) va = eu.Vector2(*a) vb = eu.Vector2(*b) w = va + vb self.assertTrue(isinstance(w, eu.Vector2)) self.assertEqual((w.x, w.y), (4.0, 9.0)) c = (11.0, 17.0) pc = eu.Point2(*c) d = (13.0, 23.0) pd = eu.Point2(*d) self.assertTrue(isinstance(va + pc, eu.Point2)) self.assertTrue(isinstance(pc + pd, eu.Vector2)) self.assertTrue(isinstance(va + b, eu.Vector2)) self.assertEqual(va + vb, va + b)
def __init__(self, x, y, batch): self.position = euclid.Point2(x, y) self.velocity = euclid.Point2(0, 0) self.angle = math.pi/2 self.batch = batch self.lines = batch.add(6, GL_LINES, primitives.SmoothLineGroup(), ('v2f', (0, 0) * 6), ('c4B', (255, 255, 255, 255) * 6)) self.ball_position = euclid.Point2(window.width/2, window.height/4) self.ball_velocity = euclid.Point2(0, 0) self.ball_lines = primitives.add_circle(batch, 0, 0, 20, (255, 255, 255, 255), 20) self._ball_verts = list(self.ball_lines.vertices) self._update_ball_verts() self.join_active = False self.join_line = None self.joined = False
def calcCar(self): self.currentSpeed = ac.getCarState(self.id, acsys.CS.SpeedKMH) #get the world position and direction x, y, z = ac.getCarState(self.id, acsys.CS.WorldPosition) self.currentWorldPosition = euclid.Point2(x, z) ff, uf, lf = ac.getCarState(self.id, acsys.CS.TyreContactPoint, acsys.WHEELS.FL) fr, ur, lr = ac.getCarState(self.id, acsys.CS.TyreContactPoint, acsys.WHEELS.RL) f, u, l = ff - fr, uf - ur, lf - lr # f,u,l now represents the vector pointing from RL wheel to FL wheel self.currentVelocityVector = euclid.Vector2(f, l).normalize() self.laps = ac.getCarState(self.id, acsys.CS.LapCount) self.splineposition = ac.getCarState(self.id, acsys.CS.NormalizedSplinePosition) #the relative Position (to the player itself) is 0,0 self.relativePositionMeters = euclid.Point2(0, 0) self.playerDistance = 0 return x, y, z
def test_swizzle_get(self): xy = (1.0, 2.0) v2 = eu.Point2(*xy) self.assertEqual(v2.x, xy[0]) self.assertEqual(v2.y, xy[1]) self.assertEqual(v2.xy, xy) self.assertEqual(v2.yx, (xy[1], xy[0])) exception = None try: v2.z == 11.0 except Exception as a: exception = a assert isinstance(exception, AttributeError)
def calcDrawingInformation(self, playerVectorReversed): global carWidth, carLength, carBorderWidth #the big goal is the determination of the centerPositionGui of this car, #as well as the opacity to draw this one #the player car is slightly easier. We start at 0,0, add the offset and multiply with zoom if self.isPlayer: self.centerPositionGui = euclid.Point2((0 + xOff) * guiZoomFactor, (0 + xOff) * guiZoomFactor) self.maxOpacity = 1.0 # 计算背景框四个点 l = carLength / 2 * worldzoom * guiZoomFactor w = carWidth / 2 * worldzoom * guiZoomFactor borderWidth = carBorderWidth * worldzoom * guiZoomFactor #front left x = self.centerPositionGui.x - w y = self.centerPositionGui.y - l self.guiBorderPtFL = [x, y] minIndex = 0 minPt = [x, y] #front right x = self.centerPositionGui.x + w y = self.centerPositionGui.y - l self.guiBorderPtFR = [x, y] if y < minPt[1]: minIndex = 1 minPt = [x, y] #rear left x = self.centerPositionGui.x - w y = self.centerPositionGui.y + l self.guiBorderPtRL = [x, y] if y < minPt[1]: minIndex = 2 minPt = [x, y] #rear right x = self.centerPositionGui.x + w y = self.centerPositionGui.y + l self.guiBorderPtRR = [x, y] if y < minPt[1]: minIndex = 3 minPt = [x, y] # Maybe it'll be useful. # Yesterday I've spent some hours drawing something about circles for my tachometer. I've discovered 2 rules to draw Quads and Triangles (ac.glBegin(2) or ac.glBegin(3)). # 1) 1st point ac.glVertex2f(x, y) must have minimal Y coordinate. # 2) other 2 or 3 points must be defined counterclockwise (as you look on it) from 1st point. self.updateGuiBorderPtListWithMinYPositionIndex(minIndex) # 计算车框四个点位置 #front left x = self.centerPositionGui.x - w + borderWidth y = self.centerPositionGui.y - l + borderWidth self.guiPtFL = [x, y] minIndex = 0 minPt = [x, y] #front right x = self.centerPositionGui.x + w - borderWidth y = self.centerPositionGui.y - l + borderWidth self.guiPtFR = [x, y] if y < minPt[1]: minIndex = 1 minPt = [x, y] #rear left x = self.centerPositionGui.x - w + borderWidth y = self.centerPositionGui.y + l - borderWidth self.guiPtRL = [x, y] if y < minPt[1]: minIndex = 2 minPt = [x, y] #rear right x = self.centerPositionGui.x + w - borderWidth y = self.centerPositionGui.y + l - borderWidth self.guiPtRR = [x, y] if y < minPt[1]: minIndex = 3 minPt = [x, y] # 打印四个点信息: # ac.log('Tyres:') # ac.log('FL: {}'.format(self.guiPtFL)) # ac.log('FR: {}'.format(self.guiPtFR)) # ac.log('RL: {}'.format(self.guiPtRL)) # ac.log('RR: {}'.format(self.guiPtRR)) # Maybe it'll be useful. # Yesterday I've spent some hours drawing something about circles for my tachometer. I've discovered 2 rules to draw Quads and Triangles (ac.glBegin(2) or ac.glBegin(3)). # 1) 1st point ac.glVertex2f(x, y) must have minimal Y coordinate. # 2) other 2 or 3 points must be defined counterclockwise (as you look on it) from 1st point. self.updateGuiPtListWithMinYPositionIndex(minIndex) else: #the other cars have to be rotated around the origin, then translated and zoomed #### #Rotation: We need the angle to the reverted playerVector angleR = math.atan2(-1, 0) - math.atan2(playerVectorReversed.y, playerVectorReversed.x) cosTheta = math.cos(angleR) sinTheta = math.sin(angleR) x = cosTheta * self.relativePositionMeters.x - sinTheta * self.relativePositionMeters.y y = sinTheta * self.relativePositionMeters.x + cosTheta * self.relativePositionMeters.y #### #Opacity: How far away is the other car - regarding y (we don't want to fade someone out who is on the same height) #self.maxOpacity = self.calcAlpha(self.playerDistance) #would be a circle around the player self.maxOpacity = self.calcAlpha(y) ##### #Overlaping indicator if showIndicators == 1: self.calcOverlap(-x, y) #### #Translation: Now we add the offsets and multiply with the zoom x = x * worldzoom * -1 y = y * worldzoom * -1 self.centerPositionGui = euclid.Point2((x + xOff) * guiZoomFactor, (y + yOff) * guiZoomFactor) # 计算当前车方向向量与玩家车方向向量的夹角 velocityVectorReverse = euclid.Vector2( self.currentVelocityVector.x * -1, self.currentVelocityVector.y * -1) currentAngleFromPlayer = velocityVectorReverse.angle( playerVectorReversed) # 计算叉乘结果 为正代表速度向量在玩家速度向量的顺时针方向 temp = velocityVectorReverse.x * playerVectorReversed.y - velocityVectorReverse.y * playerVectorReversed.x # 如果是需要逆时针旋转绘画点,则为1,顺时针则为-1 rotateDirection = -1 if temp > 0 else 1 cosValue = math.cos(currentAngleFromPlayer * rotateDirection) sinValue = math.sin(currentAngleFromPlayer * rotateDirection) # 计算背景框四个点 l = carLength / 2 * worldzoom * guiZoomFactor w = carWidth / 2 * worldzoom * guiZoomFactor borderWidth = carBorderWidth * worldzoom * guiZoomFactor #front left x = self.centerPositionGui.x - w y = self.centerPositionGui.y - l self.guiBorderPtFL = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) minIndex = 0 minPt = [x, y] #front right x = self.centerPositionGui.x + w y = self.centerPositionGui.y - l self.guiBorderPtFR = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) if y < minPt[1]: minIndex = 1 minPt = [x, y] #rear left x = self.centerPositionGui.x - w y = self.centerPositionGui.y + l self.guiBorderPtRL = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) if y < minPt[1]: minIndex = 2 minPt = [x, y] #rear right x = self.centerPositionGui.x + w y = self.centerPositionGui.y + l self.guiBorderPtRR = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) if y < minPt[1]: minIndex = 3 minPt = [x, y] # Maybe it'll be useful. # Yesterday I've spent some hours drawing something about circles for my tachometer. I've discovered 2 rules to draw Quads and Triangles (ac.glBegin(2) or ac.glBegin(3)). # 1) 1st point ac.glVertex2f(x, y) must have minimal Y coordinate. # 2) other 2 or 3 points must be defined counterclockwise (as you look on it) from 1st point. self.updateGuiBorderPtListWithMinYPositionIndex(minIndex) # 计算车框四个点位置 #front left x = self.centerPositionGui.x - w + borderWidth y = self.centerPositionGui.y - l + borderWidth self.guiPtFL = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) minIndex = 0 minPt = [x, y] #front right x = self.centerPositionGui.x + w - borderWidth y = self.centerPositionGui.y - l + borderWidth self.guiPtFR = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) if y < minPt[1]: minIndex = 1 minPt = [x, y] #rear left x = self.centerPositionGui.x - w + borderWidth y = self.centerPositionGui.y + l - borderWidth self.guiPtRL = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) if y < minPt[1]: minIndex = 2 minPt = [x, y] #rear right x = self.centerPositionGui.x + w - borderWidth y = self.centerPositionGui.y + l - borderWidth self.guiPtRR = rotatePoint([x, y], self.centerPositionGui, cosValue, sinValue) if y < minPt[1]: minIndex = 3 minPt = [x, y] # Maybe it'll be useful. # Yesterday I've spent some hours drawing something about circles for my tachometer. I've discovered 2 rules to draw Quads and Triangles (ac.glBegin(2) or ac.glBegin(3)). # 1) 1st point ac.glVertex2f(x, y) must have minimal Y coordinate. # 2) other 2 or 3 points must be defined counterclockwise (as you look on it) from 1st point. self.updateGuiPtListWithMinYPositionIndex(minIndex)
def _update_position(self): """updates vertex list""" if not self._visible: self._vertex_list.vertices[:] = [0, 0, 0, 0, 0, 0, 0, 0] return img = self._texture if self.transform_anchor_x == self.transform_anchor_y == 0: if self._rotation: x1 = -self._image_anchor_x * self._scale y1 = -self._image_anchor_y * self._scale x2 = x1 + img.width * self._scale y2 = y1 + img.height * self._scale x = self._x y = self._y r = -math.radians(self._rotation) cr = math.cos(r) sr = math.sin(r) ax = int(x1 * cr - y1 * sr + x) ay = int(x1 * sr + y1 * cr + y) bx = int(x2 * cr - y1 * sr + x) by = int(x2 * sr + y1 * cr + y) cx = int(x2 * cr - y2 * sr + x) cy = int(x2 * sr + y2 * cr + y) dx = int(x1 * cr - y2 * sr + x) dy = int(x1 * sr + y2 * cr + y) self._vertex_list.vertices[:] = [ ax, ay, bx, by, cx, cy, dx, dy ] elif self._scale != 1.0: x1 = int(self._x - self._image_anchor_x * self._scale) y1 = int(self._y - self._image_anchor_y * self._scale) x2 = int(x1 + img.width * self._scale) y2 = int(y1 + img.height * self._scale) self._vertex_list.vertices[:] = [ x1, y1, x2, y1, x2, y2, x1, y2 ] else: x1 = int(self._x - self._image_anchor_x) y1 = int(self._y - self._image_anchor_y) x2 = x1 + img.width y2 = y1 + img.height self._vertex_list.vertices[:] = [ x1, y1, x2, y1, x2, y2, x1, y2 ] else: x1 = int(-self._image_anchor_x) y1 = int(-self._image_anchor_y) x2 = x1 + img.width y2 = y1 + img.height m = self.get_local_transform() p1 = m * euclid.Point2(x1, y1) p2 = m * euclid.Point2(x2, y1) p3 = m * euclid.Point2(x2, y2) p4 = m * euclid.Point2(x1, y2) self._vertex_list.vertices[:] = [ int(p1.x), int(p1.y), int(p2.x), int(p2.y), int(p3.x), int(p3.y), int(p4.x), int(p4.y) ]
def update(self, dt): self.angle += (keyboard[key.LEFT] - keyboard[key.RIGHT]) * math.pi * dt r = euclid.Matrix3.new_rotate(self.angle) if keyboard[key.UP]: thrust = r * euclid.Vector2(600, 0) else: thrust = euclid.Vector2(0, 0) # attempt join on spacebar press s_b = self.position - self.ball_position if keyboard[key.SPACE] and abs(s_b) < 100: self.join_active = True if not self.joined: # simulation is just the ship # apply thrust to the ship directly thrust.y += GRAVITY # now figure my new velocity self.velocity += thrust * dt # calculate new line endpoints self.position += self.velocity * dt else: # simulation is of a rod with ship and one end and ball at other n_v = s_b.normalized() n_t = thrust.normalized() # figure the linear acceleration, velocity & move d = abs(n_v.dot(n_t)) lin = thrust * d lin.y += GRAVITY self.velocity += lin * dt self.cog += self.velocity * dt # now the angular acceleration r90 = euclid.Matrix3.new_rotate(math.pi/2) r_n_t = r90 * n_t rd = n_v.dot(r_n_t) self.ang_velocity -= abs(abs(thrust)) * rd * 0.0001 self.join_angle += self.ang_velocity * dt # vector from center of gravity our to either end ar = euclid.Matrix3.new_rotate(self.join_angle) a_r = ar * euclid.Vector2(self.join_length/2, 0) # set the ship & ball positions self.position = self.cog + a_r self.ball_position = self.cog - a_r self._update_ball_verts() if self.join_active: if abs(s_b) >= 100 and not self.joined: self.joined = True h_s_b = s_b / 2 self.cog = self.position - h_s_b self.join_angle = math.atan2(s_b.y, s_b.x) self.join_length = abs(s_b) # mass just doubled, so slow linear velocity down self.velocity /= 2 # XXX and generate some initial angular velocity based on # XXX ship current velocity self.ang_velocity = 0 # render the join line l = [ self.position.x, self.position.y, self.ball_position.x, self.ball_position.y ] if self.join_line: self.join_line.vertices[:] = l else: self.join_line = self.batch.add(2, GL_LINES, primitives.SmoothLineGroup(), ('v2f', l), ('c4B', (255, 255, 255, 255) * 2)) # update the ship verts bl = r * euclid.Point2(-25, 25) t = r * euclid.Point2(25, 0) br = r * euclid.Point2(-25, -25) x, y = self.position self.lines.vertices[:] = [ x+bl.x, y+bl.y, x+t.x, y+t.y, x+t.x, y+t.y, x+br.x, y+br.y, x+br.x, y+br.y, x+bl.x, y+bl.y, ]
def addBallEv(self, worldPos): ball = world.Ball(euclid.Point2(*worldPos)) self.addBall(ball)
import euclid problem_point = euclid.Point2(0.0, 30.0) problem_line = euclid.Line2(euclid.Point2(0.0, 0.0), euclid.Point2(0.0, 50.0)) problem_point.distance(problem_line)
14, 3, 7, 11, 15, ] ] blist = [b[i // 4, i % 4] for i in range(16)] if not all(qvl(v, w) for v, w in zip(alist, blist)): print('Error (' + label + '):', alist, 'vs', blist) return print(type(a), 'vs', type(b)) raise Exception('Unsupported types!') ev1 = euclid.Point2(1, 2) nv1 = npeuclid.Vec2(1, 2) aseq(ev1, nv1, '1. Point') raff = np.random.random(6) em1 = euclid.Matrix3.new_affine(raff) nm1 = npeuclid.Affine2.new_affine(raff) aseq(em1, nm1, '1. aff') aseq(euclid.Matrix3.new_rotate(1), npeuclid.Affine2.new_rotate(1), '2. rotate') aseq( euclid.Matrix3.new_rotate(np.pi / 3) * ev1, npeuclid.Affine2.new_rotate(np.pi / 3) * nv1, '3. rotate apply')
def point2(self): return euclid.Point2(self.x, self.y)
import euclid problem_point = euclid.Point2(1.0, 2.0) problem_line = euclid.Line2(euclid.Point2(1.0, 0.0), euclid.Point2(2.0, 2.0)) print problem_line.distance(problem_point)