Collision problem
Blitz3D Forums/Blitz3D Programming/Collision problem
| ||
Hi guys, I'm working on Arkanoid3D for the last 2 weeks and I'm implementing the powerups now. I can collect them and react on them, and I have a problem with 2 powerups (the ones that make your paddle smaller and larger). When the game starts, I'm using the function CreatePaddleMesh() to create the mesh that represents the paddle. The function creates the entire mesh, positions the mesh, colors the mesh, and sets collision type (ColPaddle). With the default paddle (the one that's created first), nothing's wrong. But when I pickup the powerup to make it smaller/larger, the game crashes when a ball hits the paddle. When I pickup the SmallPaddle/LargePaddle powerups, I free the original paddle entity and recreate a new one that's smaller/larger, as the CreatePaddleMesh takes the size as parameter. Function HitPaddle(Ball.TBall) Local PaddleX#, PaddleColX#, PaddleColOffset# ; Check to see if the given ball collides with the paddle (PaddleHit will hold the handle of the paddle if collision has been detected) PaddleHit = EntityCollided(Ball\Entity, ColPaddle) ; If the ball collided with the paddle If PaddleHit Then ; Turn the ball upwards TurnEntity Ball\Entity, 0, 0, EntityRoll#(Ball\Entity) * (-1) ; Get the X-position of the paddle PaddleX# = EntityX#(PaddleHit) ; Debug-info PaddleColCount = CountCollisions(PaddleHit) BallColCount = CountCollisions(Ball\Entity) BallCollidedWith = CollisionEntity(Ball\Entity, 1) PaddleHandle = Player\Paddle\Entity ; Get the global X-position where the ball hit the paddle PaddleColX# = CollisionX#(PaddleHit, 1) ; Calculate offset PaddleColOffset# = PaddleX# - PaddleColX# ; Turn the ball depending on the offset and the size of the paddle (outer edges of the paddle deflect the ball at (-)60°, in the middle 0°) TurnEntity Ball\Entity, 0, 0, PaddleColOffset# * (60.0 / Player\Paddle\Size#) EndIf End Function This is the code where it crashes. It highlights the line: PaddleColX# = CollisionX#(PaddleHit, 1) Error-message: Collision index out of range. I've added some extra debug lines to pinpoint the problem, but to no avail. The debugger lists these values: PaddleHit = 40523472 BallCollidedWith = 40523472 PaddleHandle = 40523472 BallColCount = 1 PaddleColCount = 0 That's what I find strange. PaddleHit gets the correct handle of the paddle (the ball collided with the paddle), but there are no collisions registered on the paddle (PaddleColCount = 0). But the ball has registered 1 collision (BallColCount = 1). How is this possible? |
| ||
I debugged some more. When the paddle was first created, the PaddleColCount is 1 if a ball hits the paddle. But when I replace the paddle with a new one (free the first paddle and create a second paddle), the PaddleColCount remains at 0 when a ball hits the paddle. I don't get it. To create the second paddle, I'm using exactly the same function as for creating the first paddle. If I can't get this fixed, I'll have to recreate my entire collision detection. And it's alot of code. |
| ||
I've redone the collision routines and now it's ok. I'm just using the CollisionX# on the ball instead of the paddle, because I can't get the paddle to register any collisions when it's been replaced. Try it here: http://users.telenet.be/vge/Arkanoid3D/Arkanoid3D.exe |
| ||
I think it might be something to do with source and destination entities and which is moving... OR.... You have given the second paddle entity a collision type I assume? Thios should be reset even if the handle is the same because I think the value would have changed iunternally To be honest, Blitz3D collisions seem to always give me headaches! |
| ||
I've found using EntityDistance checks works just as good. I wrote my own collision routine for Robo Attack3D using those since simply using Collisions was too much of a headache. ;Custom Bot Collison Code. For check.Bot = Each Bot If check.Bot <> Bot.bot If EntityDistance(Bot\en,check.Bot\en) <= 0.2 If EntityZ(Bot.Bot\en) < EntityZ(check.Bot\en) TranslateEntity Bot.Bot\en,0,0,-Bot\Speed# End If If EntityZ(Bot.Bot\en) > EntityZ(check.Bot\en) TranslateEntity Bot.Bot\en,0,0,Bot\Speed# End If If EntityX(Bot.Bot\en) > EntityX(check.Bot\en) TranslateEntity Bot.Bot\en,Bot\Speed# End If If EntityX(Bot.Bot\en) < EntityX(check.Bot\en) TranslateEntity Bot.Bot\en,-Bot\Speed# End If End If End If Next ;End of Bot on Bot Collision Checking This worked out great for me. Gave me perfect Bot to Bot collision :) |
| ||
Entitydistance is not option here. The paddle is only 2 units high, but can be up to 11.2 units wide. If I would use entitydistance, I would need to check if every balls comes within 5.6 units distance of the paddle. If the ball is going for the middle of the paddle, it would bounce off way above the paddle. @ Malice: The paddle has an entitytype, that's correct. I'm only using one function to create the paddle, so every paddle I create has the same parameters, entitytype, color, ... So if one paddle works, I find it very strange that the second won't work. But it's ok now, I've found a work-around by checking CollisionX on the ball, not on the paddle. It returns the same coordinates, so it's good enough and it works. |