Welcome to tridi game development

In this blog, i'll try to explain how to create game, tips and tricks to develop game, especially using my 3D engine, tridi - in English and Indonesia.

Bricks game logic

I think we will understand more if i give you a picture, here you go:




So we have 2 collision detection code for our game. First, collision with wall. This collision is simple to code, here:

if yb<-55:yb,vd=-55,(360-vd)%360
if xb<-75:xb,vd=-75,(180-vd)%360
if xb>75:xb,vd=75,(180-vd)%360

Basically that is a ball orientation modification when ball position (xb or yb) reach a limit position. % 360 (mod operator) is to ensure that vd is always in 0-359. The rules is just like light reflection we learn in physic.

Second, collision with object (box). From the picture we know that (xo,yo)-(xb,yb) is the ball motion vector. So we need this vector to check is this vector collide with one of the four side of the object (box) ?

To do it just call boxintersect with parameter is motion vector, object position and the object itself. Its return 1-4 mean on which side the vector collide.

boxintersect source code is like this. Its job to get the bounding box from tridi and create an array of line (4 line) represent each of object side. And then calculate if one of this line intersect with motion vector provided.
def boxintersect(x1,y1,x2,y2,xo,yo,o):
x1=x1-(x2-x1) # extend vector ? just to make sure ????
y1=y1-(y2-y1)
boxs=([o.bbox[0],o.bbox[2]],[o.bbox[3],o.bbox[2]],[o.bbox[3],
o.bbox[5]],[o.bbox[0],o.bbox[5]],[o.bbox[0],o.bbox[2]])
for i in range(3):
b1=boxs[i]
b2=boxs[i+1]
if lintersect(x1,y1,x2,y2,b1[0]+xo,b1[1]+yo,b2[0]+xo,b2[1]+yo):return i+1

Rendering the objects
To render objects, we need to initialize renderer, set the position of the objects(s) and submit them to tridi rendering engine. For world transformation, in this game we set the camera only once. The camera will see the coord (0,0,0) at distance (180) and from the top (0,0,0) the code is casp(0,0,0,180,0,0,0) you can try change this code to see the effect. I recommend to change the 4th and last number. Here the objects rendering code:

# draw 3d scene ===============================
tridi.scn_reset()
tridi.scn_addlights(LIGHT0)
# set object position
if vb<>0: ball.set1(xb,yb,-30,rframe*4,rframe*7,0)
else: ball.set1(xp,yp-5,-30,rframe*4,rframe*7,0)
pad.set1(xp,yp,-30,0,0,90)
wall.set1(0,13,-20,0,0,-90)
# submit objects
ball.submit()
pad.submit()
wall.submit()
# submit bricks

For bricks, we have 1 objects but we will submit it many times, depends how many bricks left on the game. Set1 parameter is (x,y,z,rota,rotb,rotc)

Player movement
To control movement of the pad, first we should get keyboard event. Keyboard event hold information about which key pressed/released. After we get the events, we can act according to the key pressed/released. The code is simple:


# handle key events =============================
x=tridi.getevents()
for e in x:
if e[0]==SDL_KEYUP:
if e[1]==SDLK_Q: running=0
if e[1]==SDLK_W and vb==0:
xb=xp
yb=yp
vb=1
vd=40+tridi.rnd(90)
if e[1] in keystate:del keystate[e[1]]
if e[0]==SDL_KEYDOWN:
keystate[e[1]]=1

if SDLK_LEFT in keystate and xp>-65: xp-=1
if SDLK_RIGHT in keystate and xp<65: xp+=1

Keystate is needed because we want to detect more than 1 key event at once. So

Tidak ada komentar: