cpctelera
cpctelera copied to clipboard
Adding basic sprite collision
What do you think about adding basic sprites collision (rectangle intersection) ? Ex : cpct_areSpritesCollide(...)
@lronaldo already make an optimized function here www.amstrad.es/forum/viewtopic.php?t=5190&p=71065. I'm currently using and it's really faster than the "classical" C function of rectangle intersection.
I have thought of it many times, but most of the time discarded it because it always requires a concrete structure for bounding box representation. Some games may use a basic (x,y,w,h) tuple as bounding box, but many others use different and specific representations. That means either developers have to adapt to CPCtelera's routines to use them, or CPCtelera has to provide many different collision detection functions, for different types. There is no way to do such thing in a generic manner.
Moreover, collision routines are faster or slower depending on the order in which they check axes. I have many recent videos of my lessons explaining this matters:
- https://www.youtube.com/watch?v=ciXJRspKzAs&feature=youtu.be (Simple Bounding Box Collision)
- https://www.youtube.com/watch?v=AinLq3i_tqQ&feature=youtu.be (Optimization tecniques for many collisions)
- Tomorrow at 12:30h, my next video shows some of the optimizations
However, it might come in handy to have just a simple collision routine for many games. For such routine I would pick up a bounding box structure like this (x, y, endx, endy) having 2 points for the upper-left and bottom-right corners of the bounding box. With such structure, and assuming numbers are always positive, collision routine becomes extremely simple:
; IX = first bounding box
; IY = second bounding box
; x, y, endx, endy = constant numbers with the offset from IX or IY
collision_check::
;;-------------------------------------------
;; FIRST PART: Y-Axis
;;-------------------------------------------
;; if ( endy(ix) < y(iy) ) THEN no-collision
;; if ( endy(ix) - y(iy) < 0 ) THEN no-collision
ld a, endy(ix) ;; [5] A = endy(ix)
sub y(iy) ;; [5] A = endy(ix) - y(iy)
ret c ;; [2/3] IF ( endy(ix) - y(iy) < 0 )
;; THEN exit no-collision
;; if ( endy(iy) < y(ix) ) THEN no-collision
;; if ( endy(iy) - y(ix) < 0 ) THEN no-collision
ld a, endy(iy) ;; [5] A = endy(iy)
sub y(ix) ;; [5] A = endy(iy) - y(ix)
ret c ;; [2/3] IF ( endy(iy) - y(ix) < 0 )
;; THEN exit no-collision
;;-------------------------------------------
;; SECOND PART: X-Axis
;;-------------------------------------------
;;.......
;;.......
;;.......
So I'm rethinking it, and it might be interesting to add a function like this to CPCtelera.
I vote for the idea. I would be happy with just one method.
Most of newbies in cpctelera use basic colisions like that one. I vote for implementing one simple, with basic entity structure