3D VARIABLE PROBLEM

Blitz3D Forums/Blitz3D Programming/3D VARIABLE PROBLEM

Ash_UK(Posted 2003) [#1]
hi guys, i was wondering if anyone could possibly help m here and point out what it is thats stopping my code from working. The problem is, when i run the code the ball keeps going forwards and won't bounce back.

Heres the code:

Graphics3D 800,600
SetBuffer BackBuffer()


Type BALL

Field x#
Field y#
Field z#
Field x_vector%
Field z_vector%
Field entity
Field collide

End Type



Type BRICK

Field x#
Field z#
Field status

End Type



;COLLISON VARIABLES
Const PLAYER_COL=1
Const WALL_COL=2
Const BALL_COL=3


;STATUS VARIABLES
Const SOLID=1
Const CLEAR=2



;SETUP CAMERA AND LIGHT
cam=CreateCamera()
light=CreateLight()



;LOAD THE SCENE
scene=LoadAnimMesh("level.3ds")
EntityType scene,WALL_COL,True

cam_piv=FindChild(scene,"cam") ;PIVOT FOR CAMERA
cam_x=EntityX(cam_piv)
cam_y=EntityY(cam_piv)
cam_z=EntityZ(cam_piv)

cam_focus=FindChild(scene,"focus") ;FOCUS POINT FOR CAMERA

bat=FindChild(scene,"bat") ;PLAYER
EntityType bat,PLAYER_COL
EntityRadius bat,15

ball.BALL=New BALL
ball\entity=FindChild(scene,"ball") ;AND FINALLY THE BALL (PHEW!!)
EntityType ball\entity,BALL_COL
EntityRadius ball\entity,10
ball\x=EntityX(ball\entity)
ball\y=EntityY(ball\entity)
ball\z=EntityZ(ball\entity)
ball\x_vector=1
ball\z_vector=1


PositionEntity cam,cam_x,cam_y,cam_z
EntityParent cam,cam_piv

PointEntity cam,cam_focus
EntityParent cam_piv,bat





;START THE COLLISIONS
Collisions PLAYER_COL,WALL_COL,2,2
Collisions BALL_COL,WALL_COL,2,2

time=CreateTimer(60)

;MAIN GAME LOOP

While Not KeyDown(1)

mouse_x=MouseXSpeed()/2

If ball\x_vector=1 Then ball\x=ball\x+2
If ball\x_vector=-1 Then ball\x=ball\x-2
If ball\z_vector=1 Then ball\z=ball\z+2
If ball\z_vector=-1 Then ball\z=ball\z-2



If KeyDown(200) Then MoveEntity cam_piv,0,0,1
If KeyDown(208) Then MoveEntity cam_piv,0,0,-1
If KeyDown(205) Then MoveEntity bat,1,0,0
If KeyDown(203) Then MoveEntity bat,-1,0,0


MoveEntity bat,mouse_x,0,0
PositionEntity ball\entity,ball\x,ball\y,ball\z


UpdateWorld

If EntityCollided(ball\entity,WALL_COL) Then ball\collide=1

If ball\collide=1 And ball\x_vector=1 Then ball\x_vector=-1:ball\collide=0
If ball\collide=1 And ball\x_vector=-1 Then ball\x_vector=1:ball\collide=0
If ball\collide=1 And ball\z_vector=1 Then ball\z_vector=-1:ball\collide=0
If ball\collide=1 And ball\z_vector=-1 Then ball\z_vector=1:ball\collide=0



RenderWorld

Text 0,0,ball\x+" "+ball\z
Flip

Wend
End



Thanks ever so much for any help

BLUE


jfk EO-11110(Posted 2003) [#2]
If ball\collide=1 And ball\x_vector=1 Then ball\x_vector=-1:ball\collide=0

I am not shure if this is going to help, but basicly the ":" is a Command seperator that has the same meaning as a line break. To make shure there is no error I would use a conventional if endif Alignement.

If ball\collide=1 And ball\x_vector=1 Then
ball\x_vector=-1
ball\collide=0
endif


Ash_UK(Posted 2003) [#3]
hmmm, i tried your idea and it's still not working. Basically the problem is, the ball\z variable will not change for some reason (to start counting backwards instead of forwards).
Thanks ever so much for your kind help jfk, i really appriciate you trying to help me out with this :-)


BLUE


EOF(Posted 2003) [#4]
From looking at the code it seems that wherever the ball hits ONLY the x direction will be altered.
Suppose the ball hits the top end of your level.
Here is what happens in your code:


a) A collision has happened so ball\collide = 1
b) One of these WILL be executed depending on the value of x_vector

If ball\collide=1 And ball\x_vector=1 Then ball\x_vector=-1:ball\collide=0
If ball\collide=1 And ball\x_vector=-1 Then ball\x_vector=1:ball\collide=0

c) ball\colide is now 0
d) The next two checks NEVER go through because ball\coillide is set to 0

If ball\collide=1 And ball\z_vector=1 Then ball\z_vector=-1:ball\collide=0
If ball\collide=1 And ball\z_vector=-1 Then ball\z_vector=1:ball\collide=0


One quick workaround for you:
If you are doing a bat & ball game and your level is square-shaped then you can get the ball to rebound without collisions.

This example assumes the level is placed in the middle of the 3d area (0,0,0)

; level width/height dimensions in blitz units
CONST levW=100 , levH=86

UpdateBallPos

; check if ball has hit walls
If ABS(EntityX(ball\entity))<=-LevW/2 ball\x_vector=-ball\x_vector
If ABS(EntityX(ball\entity))>=LevW/2 ball\x_vector=-ball\x_vector
If ABS(EntityZ(ball\entity))<=-LevH/2 ball\z_vector=-ball\z_vector
If ABS(EntityZ(ball\entity))>=LevH/2 ball\z_vector=-ball\z_vector



Ash_UK(Posted 2003) [#5]
Thanks ever so much syntax, you have helped me out a GREAT deal!!! I really appriciate it. Now i can get back to my game :-)


Thanks again

BLUE


EOF(Posted 2003) [#6]
No problem. I just knocked this up in any case:

; simple bounce example

Const sw=640,sh=480
Const ws=12 ;   wall spacing
Const inner#=9.5 ; inside area of level to bounce in

Graphics3D sw,sh,0,2
SetBuffer BackBuffer()

; make a cheap level
wallN=CreateCube() : ScaleEntity wallN,ws,1,1 : MoveEntity wallN,0,0,ws
wallE=CreateCube() : ScaleEntity wallE,1,1,ws : MoveEntity wallE,ws,0,0
wallS=CreateCube() : ScaleEntity wallS,ws,1,1 : MoveEntity wallS,0,0,-ws
wallW=CreateCube() : ScaleEntity wallW,1,1,ws : MoveEntity wallW,-ws,0,0
; ball
ball=CreateSphere() : EntityColor ball,40,250,70
; camera
cam=CreateCamera() : MoveEntity cam,0,ws*2,0 : PointEntity cam,ball
; light
light=CreateLight(1) : MoveEntity light,3,12,-4 : PointEntity light,ball


ballX#=0 :ballZ#=0 :  ballDX#=0.1 : ballDZ#=0.16


Repeat
	; check for collision with wall
	If Abs(ballX)<-inner ballDX=-ballDX
	If Abs(ballX)>inner ballDX=-ballDX
	If Abs(ballZ)<-inner ballDZ=-ballDZ
	If Abs(ballZ)>inner ballDZ=-ballDZ
	; move ball
	ballX=ballX+ballDX :ballZ=ballZ+ballDZ
	PositionEntity ball,ballX,0,ballZ
	;	
	RenderWorld
	Flip
Until KeyHit(1)

End