Repository navigation
Expand file tree
/
Copy pathscene_ui.py
More file actions
109 lines (87 loc) · 3.28 KB
/
Copy pathscene_ui.py
File metadata and controls
109 lines (87 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from PySide.QtGui import (QGraphicsScene)
from ball import Ball
from collisionManager import collisionManager
"""
Responsible for setting up the QGraphicsScene which populates
the QGraphicsView used to display the animation of all
objects simulated in the program.
"""
# graphicsView dimensions
SCENE_WIDTH = 579
SCENE_HEIGHT = 309
class SceneManager():
def __init__(self, graphicsView):
self.scene = QGraphicsScene()
self.scene.setSceneRect(0,0,SCENE_WIDTH,SCENE_HEIGHT)
graphicsView.setScene(self.scene)
# handles physics involved in collisiions/detection
self.cm = collisionManager()
# add object to simulation scene (given parameters)
# input: 'values' contains (x,y,x_vel,y_vel,mass,radius)
def addItem(self,param):
values = self.decode(param)
item = Ball(values[0],values[1],values[4],values[5])
item.set_velocity(values[2],values[3])
self.scene.addItem(item)
def addItems(self,items):
for item in items:
values = self.decode(item)
ball_obj = Ball(values[0],values[1],values[4],values[5])
ball_obj.set_velocity(values[2],values[3])
self.scene.addItem(ball_obj)
# translate parameters received by TCP
def decode(self,msg):
delims = ['r','m','yv','xv','y','x']
values = []
# traverses delimeters to extract values
for i in range(len(delims)):
index = msg.index(delims[i]) # search for delimeter
arg = msg[index + len(delims[i]) :] # extract value
values.append(int(float(arg))) # store value
msg = msg[:index] # reduce message left to parse
values.reverse()
return values
# access all items in the simulation scene
def getItems(self):
return self.scene.items()
def deleteItems(self):
return self.scene.clear()
def getItemsForTCP(self):
items = self.scene.items()
tcp_list = []
for item in items:
tcp_list.append(str(item))
return tcp_list
# determine an item's next location
def next_move(self,item):
# check whether any ball-to-ball collisions will occur
collisions = self.cm.if_ball_collision(item,self.getItems())
if (len(collisions) > 0): # colllision identified
# print '\nInside next_move()'
# print ('Item: ' + str(item) + ' List: ' )#+ str(collisions))
# for ball in collisions:
# print str(ball) #DEBUG
# obtain velocities so that balls just collide
delta_x, delta_y = self.cm.vel_to_ball(item,collisions[0])
# delta_x = item.x_vel
# delta_y = item.y_vel
self.cm.ball_to_ball(item,collisions[0])
else:
# determine if a wall will be hit in the next move
hit_wall = self.cm.if_wall_collision(item,SCENE_HEIGHT,SCENE_WIDTH)
if (hit_wall != 'NONE'): # if wall collision will occur
# determine displacement left to collide with wall
delta_x, delta_y = self.cm.vel_to_wall(item,hit_wall,SCENE_HEIGHT,SCENE_WIDTH)
# set object's velocity after impacting the wall
self.cm.ball_to_wall(item,hit_wall)
else:
#keep the ball in the same trajectory
delta_x = item.x_vel
delta_y = item.y_vel
# next x/y destination of the item in LOCAL coordinates
# require the offsets (-item.x_start and -item.y_start)
# given the fact that the item.x_pos and item.y_pos values
# represent locations in global coordinates
next_x = item.x_pos + delta_x - item.x_start
next_y = item.y_pos + delta_y - item.y_start
return next_x,next_y