fixed collision system

This commit is contained in:
Justus Jan Nico Wolff 2024-07-05 15:27:31 +02:00
parent 72d4f28cea
commit fdd3775da6
3 changed files with 38 additions and 13 deletions

View file

@ -1,4 +1,5 @@
import threading
from decimal import localcontext, Decimal, ROUND_HALF_UP
class color3:
def __init__(self, r=0, g=0, b=0):
@ -62,8 +63,13 @@ class vector2:
def __copy__(self):
return vector2(self.x, self.y)
def __tround__(self, target):
with localcontext() as ctx:
ctx.rounding = ROUND_HALF_UP
return Decimal(target).to_integral_value()
def __round__(self):
return vector2(round(self.x), round(self.y))
return vector2(self.__tround__(self.x), self.__tround__(self.y))
def __add__(self, v):
return vector2(self.x+v.x, self.y+v.y)

7
audiosys.py Normal file
View file

@ -0,0 +1,7 @@
import simpleaudio
class sound:
def __init__(self):
raise NotImplementedError()

View file

@ -4,6 +4,7 @@ import random
import string
import math
import time
import threading
#import itertools
#import multiprocessing
@ -32,12 +33,12 @@ class game:
self._width = int(canvas.winfo_reqwidth()/pixelsize)
self._pixelsize = pixelsize
self._OAP = {}
self._PAO = {}
self._objects = {}
self._visobjects = {}
self._camera = obj()
self._camera.position = vector2()
self._camera.zoom = pixelsize
self._OAPUPDATING = False
"""self._workers = []
self._processes = 2 # total number of processes.
for i in range(self._processes):
@ -85,8 +86,9 @@ class game:
else:
return []
def addobj(self, target: obj, physics=True):
temp = self.genid()
def addobj(self, target: obj, physics=True, id=None):
if id == None: temp = self.genid()
else: temp = id
if physics: self._objects[temp] = target
self._visobjects[temp] = target
target.id = temp
@ -117,8 +119,8 @@ class game:
if obj2.anchored == True:
obj1.velocity = vector2()
else:
if xblock: obj1.velocity.x = obj1.velocity.x/2;obj2.velocity.x = obj2.velocity.x/2
if yblock: obj1.velocity.y = obj1.velocity.y/2;obj2.velocity.y = obj2.velocity.y/2
if xblock: obj1.velocity.x = obj1.velocity.x/2;obj2.velocity.x = obj2.velocity.x/2;obj2.position.x += obj2.velocity.x
if yblock: obj1.velocity.y = obj1.velocity.y/2;obj2.velocity.y = obj2.velocity.y/2;obj2.position.y += obj2.velocity.y
return (xblock, yblock)
def calcphysobj(self, target: obj):
@ -135,13 +137,23 @@ class game:
temp = self.handlecollision(target, i)
if temp[0] == False: target.position.x += oldvel.x
if temp[1] == False: target.position.y += oldvel.y
if len(colliding) == 0: target.position += target.velocity
if len(colliding) <= 1: target.position += target.velocity
else:
target.position += target.velocity
target.velocity += target.acceleration
self.updateobjinoap(target)
def updallobjpositions(self):
self._OAPUPDATING = True
for i in self._visobjects.values():
try:
self.updateobjinoap(i)
except Exception as e:
pass
self._OAPUPDATING = False
def render(self):
if self._OAPUPDATING == False: threading.Thread(target=self.updallobjpositions).start()
self._canvas.delete(tk.ALL)
self._height = int(canvas.winfo_reqheight()/self._camera.zoom)
self._width = int(canvas.winfo_reqwidth()/self._camera.zoom)
@ -154,25 +166,25 @@ class game:
self._canvas.create_rectangle(x*self._pixelsize, y*self._pixelsize, x*self._pixelsize+self._pixelsize, y*self._pixelsize+self._pixelsize, fill=obj.color.__tohex__(), width=0)
def updobjs(self):
start = time.time()
for i in self._objects.values():
self.calcphysobj(i)
return time.time()-start
if not "__CMD__" in globals():
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.grid()
temp = game(canvas)
for i in range(200):
for i in range(10):
tempobj = obj()
tempobj.position = vector2(5, i)
tempobj.anchored = False
tempobj.velocity.x = 1
tempobj.collide = True
temp.addobj(tempobj)
while True:
temp.updobjs()
for i in temp._objects.values():
i.color += color3(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
#i.position = vector2(random.randint(0, temp._width), random.randint(0, temp._height))
took = temp.updobjs()
temp.render()
root.update()
time.sleep(0.1)
time.sleep(0.1-took)