Collision problem
Blitz3D Forums/Blitz3D Beginners Area/Collision problem
| ||
Hi guys :o) I am having a bit of a crisis with my code. When the ball hits the right side of the board, it doesn't reverse its direction for some reason. I have the code to print out the ball_x_speed variable, but it just displays an alternating number instead between 0 and 1. here is my code: Graphics3D 800,600 SetBuffer BackBuffer() Const BALL_COL = 1 Const BOARD_COL = 2 Global CAMERA = CreateCamera() PositionEntity CAMERA,0,54,-60 RotateEntity CAMERA,45,0,0 Global OMNI = CreateLight() AmbientLight 170,170,170 Global BOARD = LoadMesh("models/game_board.3ds") EntityType board,BOARD_COL Global ball_x# = 0 Global ball_x_speed = 1 Global ball_z# = 0 Global ball = CreateSphere() EntityType ball,BALL_COL EntityRadius ball,4 ScaleEntity ball,2,2,2 Collisions BALL_COL,BOARD_COL,2,2 While Not KeyHit(1) Cls PositionEntity ball,ball_x,2,ball_z ball_x = ball_x+ball_x_speed MoveEntity ball,ball_x_speed,0,0 If EntityCollided(ball,BOARD_COL) Then ball_x_speed = 1-ball_x_speed UpdateWorld RenderWorld Text 0,0,ball_x_speed Flip Wend End Thanks guys! |
| ||
Should it not be ball_x_speed = 0 - ball_x_speed? |
| ||
Try the followingIf EntityCollided(ball,BOARD_COL) Then ball_x_speed = ball_x_speed - 1 |
| ||
Gfk :o) That doesn't seem to change the problem i'm afraid nrasool :o) Erm, that does reverse the direction, but when the ball hits the left hand side it stays there and the ball_x_speed is decreasing. Thanks for your help though guys :o) |
| ||
Without seeing your board model this is a bit of educated guesswork. You're using sliding collisions - have you tried using stop collisions instead? Its possible that after the initial collision the ball is always in contact with the board and that could be screwing things up. Other than that, post the model in a ZIP file and I'll have a play. |
| ||
It would be ball_x_speed = 0 - ball_x_speed or just *-1 which is the same in this case. But the important thing is that you use positionentity and moveentity on the same entity. Thats the reason it can't get away anymore because if it collided it "turns around" but in the next loop you put it to the old position once again so it turns straight away again and will turn back to the board once again. |
| ||
Hmm :o) Here you go, this is a zip of the code with models. This may give you a better idea: Breakout start Thanks guys! |
| ||
Fixed 1. My original bug fix was correct, except for: 2. You were doing collision-dependent stuff BEFORE UpdateWorld 3. Took out the redundant TurnEntity command - it wasn't doing anything! [edit] Oh, I changed it to Stopcollisions, too. If you don't do this, the ball will slide along walls when it hits them at angles. Graphics3D 1024,768 SetBuffer BackBuffer() Const BALL_COL = 1 Const BOARD_COL = 2 Global CAMERA = CreateCamera() PositionEntity CAMERA,0,54,-60 RotateEntity CAMERA,45,0,0 Global OMNI = CreateLight() AmbientLight 190,190,190 Global BOARD = LoadMesh("models/game_board.3ds") EntityType board,BOARD_COL Global ball_x# = 0 Global ball_x_speed = 1 Global ball_z_speed = 1 Global ball_z# = 0 Global ball = CreateSphere() EntityType ball,BALL_COL EntityRadius ball,4 ScaleEntity ball,2,2,2 MoveEntity ball,0,2,0 Collisions BALL_COL,BOARD_COL,2,1 While Not KeyHit(1) Cls ball_x = ball_x+ball_x_speed ; TurnEntity board,0,0,0 MoveEntity ball,ball_x_speed,0,0 UpdateWorld If EntityCollided(ball,BOARD_COL) Then ball_x_speed = 0-ball_x_speed RenderWorld Text 0,0,ball_x_speed Flip Wend End |
| ||
Excellent :D Thankyou very much Gfk, I really appriciate your help. Thanks again! :o) |
| ||
Just a small point: instead ofball_x_speed = 0-ball_x_speed you can simply do ball_x_speed = -ball_x_speed This will have no effect other than to make your code a little bit more understandable. Possibly. :P |
| ||
I don't understand... :oP |
| ||
/me slaps Ross around a bit with a large trout :P |
| ||
Just a small point: instead of Suppose thats a matter of preference. I find it clearer to read *with* the zero. ball_x_speed = 0-ball_x_speed you can simply do ball_x_speed = -ball_x_speed This will have no effect other than to make your code a little bit more understandable. Possibly. :P |
| ||
Yeah, it's definately just a matter of preference. I'm not even sure why I mentioned the aternative way, really. :) P.S. /me slaps Ross around a bit more with a large trout :P |
| ||
:( |
| ||
You had enough of my trout, yet? I have plenty more. You have been warned! :P |