-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
345 lines (244 loc) · 10 KB
/
main.py
File metadata and controls
345 lines (244 loc) · 10 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
'''-------------------------------------------
Inverse Kinematics Test
----------------------------------------------
Simple inverse kinematics test.
Created by ultimatech
Version: Beta 1.0
2022
-------------------------------------------'''
# ------------ Import modules --------------
# Standard imports
from math import *
from pickle import TRUE
from time import time, sleep
from win32api import EnumDisplayDevices, EnumDisplaySettings, GetKeyState
# Non-standard imports (pip install might be needed)
from graphics import *
from pyautogui import position as mousePos
import keyboard
# ----------- Initialize variables -----------
class window:
width = 1080
height = 1080
vertical_resolution = 256
horizontal_resolution = 256
ratio = height / width
class FPS:
value = int()
maxValue = 'auto' # Leave 'none' for benchmarking
timer = 0
counter = object
if maxValue == 0 or maxValue == 'none':
maxValue = 999
elif maxValue == 'auto':
maxValue = getattr(EnumDisplaySettings(
EnumDisplayDevices().DeviceName, -1), 'DisplayFrequency')
lastValue = maxValue
# Clock for FPS display
start_clock_time = time.time()
last_clock_time = 0
# Used to converts rgb values into hex
def rgb(red, green, blue):
hexValue = '#%02x%02x%02x' % (red, green, blue)
return hexValue
# Joint chain and render settings
class setting:
regularChain = True
chainLength = 20 # Number of joints
jointLength = 60 # Length of each joint
showJointCounter = True
showVectors = False
showLastVector = True
colorFade = True # Fade color between each joint
color = rgb(63,93,178) # Accepts color name, hex value or rgb value using: rgb(red, green, blue); Used as default if fade is off
sizeFade = True
maxWidth = 10 # Max width of fade; used as default if fade is off
instantUpdate = False # Update joints instantly
showTrail = False # Only recommended for small amount of joints
trailLength = chainLength*15 # Number of points in trail
trail = []
origin = (-300,0) # Origin of chain
showOrigin = True
showTarget = True
showFPS = True
# Sets the origin point of the chain
originPX, originPY = setting.origin[0], setting.origin[1]
# Sets the joint chain; format: [[length1,x1,y1], [length2,x2,y2], ...]
if setting.regularChain:
joints = [[setting.jointLength, [originPX, originPY+setting.jointLength*chainJoint]]for chainJoint in range(setting.chainLength+1)]
else:
joints = [[200, [0, 200]], [200, [0, 400]], [200, [0, 600]], [200, [0, 800]], [200, [0, 1000]], [200, [0, 1200]], [200, [0, 1400]], [200, [0, 1600]], [200, [0, 1800]], [200, [0, 2000]], [200, [0, 2200]], [200, [0, 2400]]]
# Setups render window
render = GraphWin('render', window.width, window.height, autoflush=False)
render.setCoords(-window.width, -window.height, window.width, window.height)
# Sets default target
targetX, targetY = 1000, 0
gotoX, gotoY = 1000, 0
# Rotates a point counterclockwise by a given angle around a given origin.
def rotateJoint(origin, point, shiftAngle):
originX, originY = origin[1][0], origin[1][1]
pointX, pointY = point[1][0], point[1][1]
shiftX = originX + cos(shiftAngle) * (pointX - originX) - sin(shiftAngle) * (pointY - originY)
shiftY = originY + sin(shiftAngle) * (pointX - originX) + cos(shiftAngle) * (pointY - originY)
return [origin[0], [shiftX, shiftY]]
# ---------------- Unrenderer ----------------
def clear():
# Undraws all faces for next frame
for item in render.items[:]:
item.undraw()
# ---------------- Renderer ------------------
def refresh():
# ------------- Pre-rendering ------------
# Clears the screen
clear()
global FPS, last_clock_time
clock_time = time.time() - start_clock_time
FPS.timer = clock_time - last_clock_time
FPS.value += 1
# --------------- Rendering --------------
global joints, targetX, targetY, originPX, originPY
# Draws the trail
if setting.showTrail:
for _trail in joints:
setting.trail.append(Point(_trail[1][0],_trail[1][1]))
if len(setting.trail) == setting.trailLength:
del setting.trail[0]
for _trail in range(len(setting.trail)-1,0,-1):
trailFade = rgb(255,255-int((_trail+1)/len(setting.trail)*255),255-int((_trail+1)/len(setting.trail)*255))
#setting.trail[_trail].draw(render).setFill(trailFade)
if (_trail != 0) and not(setting.trail[_trail].x == originPX and int(setting.trail[_trail].y) == originPY):
Line(setting.trail[_trail], setting.trail[_trail-1]).draw(render).setFill(trailFade)
# Draws the joints
for _joint in range(len(joints)):
if _joint == 0:
jointOrigin = Point(originPX, originPY)
else:
jointOrigin = Point(joints[_joint-1][1][0],joints[_joint-1][1][1])
jointTarget = Point(joints[_joint][1][0],joints[_joint][1][1])
jointLine = Line(jointOrigin, jointTarget)
jointWidth = 3*abs(1-(len(joints)/(_joint+1)))+1
if setting.sizeFade:
if jointWidth > setting.maxWidth:
jointWidth = setting.maxWidth
else:
jointWidth = setting.maxWidth
jointLine.setWidth(jointWidth)
if setting.showVectors:
jointLine.setArrow('last')
if setting.showLastVector and _joint == len(joints)-1:
jointLine.setArrow('last')
if setting.colorFade:
jointLine.setFill(rgb(64,128,round(abs((_joint+1)/len(joints))*255)))
else:
jointLine.setFill(setting.color)
jointLine.draw(render)
# Refresh the joints positions
for _joint in range(len(joints)-1,-1,-1):
if _joint == len(joints)-1:
anchor = (targetX, targetY)
else:
anchor = (joints[len(joints)-1][1][0],joints[len(joints)-1][1][1])
if _joint == 0:
origin = (originPX,originPY)
else:
origin = (joints[_joint-1][1][0],joints[_joint-1][1][1])
target = (targetX, targetY)
# Calculates the angle towards the target
angle = -atan2((anchor[0]-origin[0]) , (anchor[1]-origin[1]))+pi/2
targetAngle = -atan2((target[0]-origin[0]) , (target[1]-origin[1]))+pi/2
shiftAngle = targetAngle - angle
# Updates the joint position
if _joint == len(joints)-1:
joints[_joint][1][0] = origin[0] + joints[_joint][0] * cos(angle)
joints[_joint][1][1] = origin[1] + joints[_joint][0] * sin(angle)
else:
for __joint in range(_joint,len(joints)):
if _joint == 0:
joints[__joint] = rotateJoint([joints[0][0],[originPX,originPY]], joints[__joint], shiftAngle)
else:
joints[__joint] = rotateJoint(joints[_joint-1], joints[__joint], shiftAngle)
# ------------------ UI ------------------
# Draws mouse position
mouse = Circle(Point(mouseX, mouseY), 10)
if mousePressed:
mouse.setFill('blue')
mouse.draw(render)
# Draws target position
if setting.showTarget:
Line(Point(gotoX-10, gotoY), Point(gotoX+10, gotoY)).draw(render), Line(Point(gotoX, gotoY-10), Point(gotoX, gotoY+10)).draw(render)
# Draws origin
if setting.showOrigin:
origin = Circle(Point(originPX, originPY), 5)
origin.setFill('red')
origin.draw(render)
# Draws Joint counter
if setting.showJointCounter:
jointCounter = Text(Point(window.width - 35 - (log(round(setting.chainLength/10)*10+9))*10, window.height-25), str(setting.chainLength) + " joints")
jointCounter.draw(render)
# Sets and displays current FPS
if FPS.timer > 1:
FPS.lastValue = FPS.value
FPS.value = 0
last_clock_time = clock_time
FPS.counter = Text(Point(-window.width+50+len(str(FPS.lastValue))* 9, window.height-25), "FPS:"+str(FPS.lastValue))
if FPS.lastValue <= FPS.maxValue/3:
FPS.counter.setTextColor("red")
elif FPS.lastValue <= FPS.maxValue/1.5:
FPS.counter.setTextColor("orange")
else:
FPS.counter.setTextColor("green")
if setting.showFPS:
FPS.counter.draw(render)
while True:
# Gets mouse position
renderPos = [int(content) for content in render.master.winfo_geometry().split("+",1)[1].split("+",1)]
mouseX = (mousePos()[0] - renderPos[0])*2 - render.width - 16
mouseY = (-mousePos()[1] + renderPos[1])*2 + render.height + 64
if GetKeyState(0x01) < 0:
mousePressed = True
gotoX, gotoY = mouseX, mouseY
else:
mousePressed = False
if setting.instantUpdate:
targetX, targetY = gotoX, gotoY
else:
targetX, targetY = targetX + (gotoX-targetX)/20, targetY + (gotoY-targetY)/20
# Render frames
clock_time = time.time() - start_clock_time
refresh()
update(FPS.maxValue*1.05)
# Prevents FPS from affecting inputs
inputSpeed = 1.0 / ((FPS.lastValue+0.01) / 120)
# ------------------ Inputs ------------------
# Render options
if keyboard.is_pressed('&'):
if not(setting.showLastVector):
setting.showLastVector = True
else:
setting.showLastVector = False
while keyboard.is_pressed('&'):
sleep(0.1)
if keyboard.is_pressed('é'):
if not(setting.showTarget):
setting.showTarget = True
else:
setting.showTarget = False
while keyboard.is_pressed('é'):
sleep(0.1)
if keyboard.is_pressed('"'):
if not(setting.instantUpdate):
setting.instantUpdate= True
else:
setting.instantUpdate = False
while keyboard.is_pressed('"'):
sleep(0.1)
if keyboard.is_pressed("'"):
if not(setting.showTrail):
setting.showTrail = True
else:
setting.showTrail = False
while keyboard.is_pressed("'"):
sleep(0.1)
# Closes window
if keyboard.is_pressed('esc'):
render.close()