Breakout physics using Collision Normals
Blitz3D Forums/Blitz3D Programming/Breakout physics using Collision Normals
| ||
Okay. I'm making a breakout game in blitz3d, it's a standard top down one, meaning that the Z-axis has no bearing on ball physics. I posted in the beginner forums and was told to use Normals for the physics to check for the angle and such when rebounding off a surface. It's just I have no clue how this works and looking through the tutorials included with blitz3d and one of the sample programs, but I haven't been able to recreate it. Heres the code that I have so far.Graphics3D 640,480,16,1 font=LoadFont("Arial",32,True) SetFont font SetBuffer BackBuffer() blurtex=CreateTexture(0,0,0) camera=CreateCamera() CameraClsMode camera,0,1 sprite=CreateSprite(camera) MoveEntity sprite,0,0,640 ScaleSprite sprite,640,640 EntityOrder sprite,-1 ;EntityTexture sprite,blurtex EntityAlpha sprite,0.75 EntityBlend sprite,3 Const CUBE_COL=1 Const SPHERE_COL=2 ;create ball Global ball=CreateSphere(12) ScaleEntity ball,3,3,3 EntityColor ball,100,100,255 EntityShininess ball,.2 Global balldirection = 0 ;create paddle Global p1=CreateCube() ScaleEntity p1,8,2,2 EntityColor p1,50,60,20 ;setup Collisions table EntityType ball,SPHERE_COL EntityType p1, CUBE_COL Collisions ball,p1,2,2 ground=CreateSphere(32) EntityColor ground,0,0,0 EntityType p1,2 While Not KeyHit(1) If count=3 CameraViewport camera,0,0,50,50 RenderWorld CopyRect 0,0,256,256,0,0,BackBuffer(),TextureBuffer(blurtex) CameraViewport camera,0,0,GraphicsWidth(),GraphicsHeight() count=0 EndIf count=count+1 MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 PositionEntity camera,0,100,1 RotateEntity camera,80,0,0 If x1#<-180 Then x1#=-180 If x1#>180 Then x1#=180 PositionEntity camera,0,0,200 RotateEntity camera,178,0,0 PositionEntity p1,x1#,122,0 MoveEntity ball,.5,.5,0 If EntityCollided(ball,p1) Then AlignToVector ball, CollisionNX (p1, 1), CollisionNY (p1, 1),CollisionNZ(p1,1), 3 EndIf UpdateWorld RenderWorld Color 0,0,0 Flip x1#=x1#-MouseXSpeed() Wend End this gives me a memory allocation error. Egh. I've been messing with it for about two hours now and no dice. Also I was wondering if anyone knows of a post or link that explains normal collision detecting in detail. |
| ||
just a quick scan but shouldn't it be? If EntityCollided(ball,p1) Then AlignToVector ball, CollisionNX (ball, 1), CollisionNY (ball, 1),CollisionNZ(ball,1), 3 EndIf |
| ||
Memory allocation error, change Collisions ball,p1,2,2 to Collisions SPHERE_COL,CUBE_COL,2,2 |
| ||
The thing is you don't change the balls direction even though you try to realign the vector. You are telling it to move x=.5 and y=.5 here:; this is the culprit MoveEntity ball,.5,.5,0 ; you always tell it to continue on this course I am having the same problem. -RZ |
| ||
I hate the game :) However the ball bounce stuff was all worked out here some time ago by the likes of SSwift. Still on the forums if you search for it. I have changed your code to include it, and tried adding paddle forces, but that bit needs improving.Graphics3D 640,480,16,1 HidePointer() camera=CreateCamera() light=CreateLight() LightColor light,100,100,100 RotateEntity light,180,0,0 Const CUBE_COL=1 Const SPHERE_COL=2 ;create ball Global ball=CreateSphere(12) ScaleEntity ball,3,3,3 EntityColor ball,100,100,255 EntityShininess ball,.5 maxballspd#=5 ;create paddle Global p1=CreateCube() ScaleEntity p1,20,3,2 EntityColor p1,200,200,0 ;setup Collisions table EntityType ball,SPHERE_COL EntityType p1, CUBE_COL Collisions SPHERE_COL,CUBE_COL,2,2 arena=CreateCube() EntityColor arena,200,100,0 FlipMesh arena : ScaleEntity arena,200,150,5 EntityType arena,CUBE_COL Local vx#=2,vy#=2 While Not KeyHit(1) x1#=x1#+MouseXSpeed()*0.5 MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 oldx#=EntityX(p1) If x1#<-180 Then x1#=-180 If x1#>180 Then x1#=180 PositionEntity p1,x1#,122,0 PFx#=EntityX(p1)-oldx PositionEntity camera,0,0,200 RotateEntity camera,180,0,0 If EntityCollided(ball,CUBE_COL) Then Nx# = CollisionNX(ball, 1) Ny# = CollisionNY(ball, 1) ; Compute the dot product of the entity's motion vector and the normal of the surface collided with. VdotN# = Vx# * Nx# + Vy# * Ny# ; Calculate the normal force. NFx# = -2.0 * Nx# * VdotN# NFy# = -2.0 * Ny# * VdotN# ; Add the normal force to the direction vector. ; if coll with paddle include some paddle force If CollisionEntity(ball,1)=p1 Then Vx# = Vx# + NFx# + PFx#*0.5 Vy# = Vy# + NFy# - Abs(PFx#*0.1) Else Vx# = Vx# + NFx# Vy# = Vy# + NFy# EndIf EndIf ;limit ballspeed ballspd# = Sqr(Vx# * Vx# + Vy# * Vy#) If ballspd# > maxballspd# Then Vx# = Vx# / ballspd# * maxballspd# Vy# = Vy# / ballspd# * maxballspd# EndIf TranslateEntity ball,Vx,Vy,0 PositionEntity ball,EntityX(ball),EntityY(ball),0 ; stay at the same Z UpdateWorld RenderWorld Flip Wend End |
| ||
OK two things... 1. Great job so far... in terms of game programming you are almost finished... well half done. By that I mean the real hard part is behind you (the ball and paddle.) 2. It is really only the simple things that have to be set now. Sound is a matter of selecting sounds for events and inserting PLAYSOUND at the proper location. Background music if you want. Drawing the targets could be accomplished with types... OR as DATA so you could change position and color each level (if you decide to have levels.) It is housekeeping really. Keep up the good work. What you learn by solving problems on this game will help you in all Blitz. I added a few commented out lines... stuff to show you how really close you are to the end... it took me about 15 min. and I don't claim to be a genius... OH my new suggestion... MORE RANDOM ANGLES off the paddle! -RZ |