I need directions

Blitz3D Forums/Blitz3D Beginners Area/I need directions

olo(Posted 2008) [#1]
hi, i am working on a pong type game (heard its good for beginners) but encountered some problems. I have already got the ball bouncing off the walls and the player/computer, but the game so far is really booring; the ball never changes direction unless someone loses. Does anyone know what i could do to make the ball move forwards and up or forwards and down, accordingly to which side of the paddle it hits?

here is my code so far (dont laugh..):
Graphics3D 1280,800
SetBuffer BackBuffer()

light=CreateLight()

cam=CreateCamera()

SeedRnd MilliSecs()

;collisions
Const type_lefts=1
Const type_ball=2
Const type_rights=3
Const type_top=4
Const type_bottom=5
Const type_player=6
Const type_computer=7



player=CreateCube()
PositionEntity player, -4.5,0,5
ScaleEntity player, 0.2,1.1,0.1
EntityType player, type_player
EntityRadius player, 0.1



computer=CreateCube()
PositionEntity computer, 4,0,5
ScaleEntity computer, 0.2,1.1,0.1
EntityType computer, type_computer
EntityRadius computer, 0.1



;ball
Global sphere_x# = 0
Global sphere_x_speed# = 0.1
Global sphere_y_speed# = 0.2
Global sphere_y# = 0
Global sphere = CreateSphere()
ScaleEntity sphere, 0.2,0.2,0.2
EntityType sphere, type_ball
EntityRadius sphere, 0.1
PositionEntity sphere, 0,0,5



;collisions
Collisions type_ball, type_lefts, 2, 1
Collisions type_ball, type_rights, 2, 1
Collisions type_ball, type_top,2,1
Collisions type_ball, type_bottom, 2, 1

Collisions type_ball, type_player, 2, 1
Collisions type_ball, type_computer, 2, 1





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
top=CreateCube()
PositionEntity top, 0,4.5,5
ScaleEntity top, 6,0.5,1
EntityType top, type_top
EntityAlpha top, 0

bottom=CreateCube()
PositionEntity bottom, 0,-4.3,5
ScaleEntity bottom, 6,0.5,1
EntityType bottom, type_bottom
EntityAlpha bottom, 0

lefts=CreateCube()
PositionEntity lefts, -6.5,0,5
ScaleEntity lefts, 0.5,4,1
EntityAlpha lefts, 0
EntityType lefts, type_lefts

Rights=CreateCube()
PositionEntity rights, 6.5,0,5
ScaleEntity rights, 0.5,4,1
EntityAlpha rights, 0
EntityType rights, type_rights
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




;THE SCORES
score1=0
score2=0






;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
While Not KeyDown(1)


;moving the player
If KeyDown(200) Then MoveEntity player, 0,0.1,0
If KeyDown(208) Then MoveEntity player, 0,-0.1,0

sphere_y = sphere_x + sphere_y_speed#
sphere_x = sphere_x + sphere_x_speed#

MoveEntity sphere, sphere_x_speed#,sphere_y_speed#,0















;adding the points (1 by 1)
;point for the computer
oldpoint1 = point1
point1 = (EntityCollided (sphere, type_lefts) <> 0) 

If point1 And (Not oldpoint1) Then 
	score2=score2+1
	
EndIf

;point for the player
oldpoint2 = point2
point2 = (EntityCollided (sphere, type_rights) <> 0) 

If point2 And (Not oldpoint2) Then 
	score1=score1+1
	
EndIf










UpdateWorld

;bounce off the walls when hit
If EntityCollided(sphere, type_lefts) Then sphere_x_speed# = 0-sphere_x_speed#
If EntityCollided(sphere, type_rights) Then sphere_x_speed# = 0-sphere_x_speed#


If EntityCollided(sphere, type_top) Then sphere_y_speed# = 0-sphere_y_speed#
If EntityCollided(sphere, type_bottom) Then sphere_y_speed# = 0-sphere_y_speed#


If EntityCollided(sphere, type_player) Then sphere_x_speed# = 0-sphere_x_speed#
If EntityCollided(sphere, type_player) Then sphere_y_speed# = 0-sphere_y_speed#

If EntityCollided(sphere, type_computer) Then sphere_x_speed# = 0-sphere_x_speed#
If EntityCollided(sphere, type_computer) Then sphere_y_speed# = 0-sphere_y_speed#


RenderWorld


Text 150,10,"player1: "+score1
Text 150,20,"Computer: "+score2
Text 0,0,"ball x speed: "+sphere_x_speed#

Flip

Wend


End


thanks


Ross C(Posted 2008) [#2]
First of all, you will need to change your approach a little, and introduce an angle variable. Because your doing this in 3d, the maths are a little easier here.

And there are two approaches to doing this too. When a collision occurs, blitz returns some information about that collision, via CollisionNX() NY() and NZ(). These are basically normals, the angle the ball hit the wall/bat at.

Basically your ball has an angle, or direction. When hitting a wall, you must find the opposite angle.

Using the normals, you simply align to vector the ball, once you figure out the angle it should bounce at. I can't quite figure it out in my head without running some code, but that is the basics.

There should be a way, of easily changing the angle, based on the angle the ball is going at. It probably will involve the angle the balls travelling at and the collision normal returned.

If no-one else replies, i'll try and knock some code together when i get back from football.


blade007(Posted 2008) [#3]
i found a typeo in it
sphere_y = sphere_x + sphere_y_speed#
sphere_x = sphere_x + sphere_x_speed#

You acidently wrote sphere_x on the first line


good luck dude ;)


olo(Posted 2008) [#4]
nope, that didnt change anything.

thanks anyway for pointing that out


Cp(Posted 2008) [#5]
I'm working on something for this code. :)
I'll be done in a bit, then ill post.


Cp(Posted 2008) [#6]
I tried to over-complicate tings and it took a while. it was actually pretty simple.If you need help with AI(which it will need) tell me.



Look at the wall collisions code.
Notice:
If EntityCollided(sphere, type_player) Then sphere_y_speed# = 0-sphere_y_speed# + Rnd(0,0.1);notice this addition
If EntityCollided(sphere, type_computer) Then sphere_y_speed# = 0-sphere_y_speed# + Rnd(-0.1,0);notice this addition

Hint for ai: make it follow the ball up and down.This has to do with y coords.


olo(Posted 2008) [#7]
thanks so much, it is much better now. I will try making the AI myself first and when i get stuck i'll ask you.
Thanks again ;)


Cp(Posted 2008) [#8]
No problem.