# How can I get the point of collision?

Hi.
I am trying to make something and I can’t figure out how to get the position where the collision has happened.

I don’t think so, especially not when using print (dir(collisonsensor))
I think you’d need to work out positions of meshes etc.
What are you trying to do?

This is the first game I ever played on a PC. I want to make something similar in Blender and then make it playable on my blog using Burster Plugin.
The problem is in physics, I can’t get the point of collision to calculate the direction where the ball should bounce.

All i can suggest is make the brick out of parallelograms or hope it doesnt hit a corner.

I will probably end up using Circles or Hexagons.

Josip, this game does not use the physics engine. You can do the same thing and calculate the positions by yourself.

I am not using any Physics, I am scripting everything in python. That is why I have this problem. I don’t know how to calculate this.
Can you help me with some links or samples?

You shouldn’t need the co-ordinates, just the vector of the ball when it hits the brick, then manipulate this using an algorithm and apply the new vector force… [getLinearVelocity(), manipulate the vector, for example, a crude inverse of the y vector, then setLinearVelocity(new_Vec)]

Hope this helps, you may want to make a slightly better vector transformation function though :D.

A crude way (in fact, the only way) to get the PoC is to use the ray sensor and its getHitPosition() and even (to make a more accurate vector function) getHitNormal()… (perhaps make a ghosted object and parent it to the ball, then have it align along the movement vector so that the ray it casts always points to the only possible collision point (maybe two extra rays to detect the sides in case it clips the corner of a brick) But like I said: crude. (This would need to be edited for 2.5 if that’s what your using, but I hear there are new API docs somewhere, so Google should help with that)…

PS: there is an example of brick-breaker in the 2.49 Physics suite on the Blender website (it’s more 3D than the classic version, but it works :D)

That’s what i thought, but i couldnt think of a function to get the hit normal! Thanks Haker23

@Haker23
I need the coordinates to calculate the vector from the center of the ball to the point of collision. When i get the vector I know how to calculate everything else.

I have tried detecting one ray, using the movement vector. Did not work, to much glitches and rarely detects side collisions.
I tried 4 vectors, for every positive and negative axis direction. Did not work with corners.

Now I want to figure this thing out and make it how it should be done. Collision detection is something used in every game engine, i am surprised there is so little explanations on how to do it on the internet.

@agoose77
If I have a vector I can get the normal by shooting a ray in that direction.

I see, the best hack I managed to come up with for this kind of thing (when I tried to make my in game water be effected by collisions) I had to access the vertex data and find out which vertex was closest to the collision point and flag it/them (using vertex paint) you could do the same but just add your process instead of flagging the vertices. I don’t think there is a way to get the co-ordinates of a collision using the sensor.

HOWEVER, if you go through the api or the python modules (ONE good thing about GPL) perhaps you could try and utilise the same method that the module uses for ray.getHitPosition(), provided the ray sensor directly uses the collision detection (which I’ve heard it does)…

The usual method for this is to use collision empties (very hacky and inefficient) and my vertex comparison method had trouble converting vertex co-ords to global co-ords…

I agree it is quite surprising the lack of this capability…

Thanks Haker23.
I will make a test with the vertex based collision to see how fast it goes.

Update:
Its really slow, crashes sometimes if I add a lot of objects, and if collision is detected ball bounce in one of 8 directions. After 2-3 minutes of testing the 8 directions is really noticeable and annoying. I get more directions if I add more veritces to the ball but the more vertices the lower is the framerate.

I found another way of doing it, if it works I will upload a blend file.

Fair enough, although I had a 32x32 (1024 verts!) grid working well and my machine is quite poor…

Just to check, how exactly did you implement it?

You should have the collision sensor hooked up to activate the script (no pulsing).
In the script should be a for loop (range between 0 and the getVertexArrayLength() result) then get the vertex position, add the overall object co-ordinates to each element (x,y,z) to get the global co-ords of the vert, then compare with the centre of the collided object (take them away and test the result, only do 2 tests of this (one for the X element and one or the Y element of the two locations or even a dot-cross product of the vectors) then when your test (which is to compare the result of the previous brackets to a predefined value, experimenting will let you tweak this to a good distance) proves positive trigger an if statement below all that, which contains your stuff for changing the vector of the ball…

should look like this:

for stuff()
____def variables
____trigger variable = 0
____if distance tests
________if they prove positive, trigger variable = 1
____if trigger variable == 1

(____ are used to show the nesting)

This script should be applied on only the ball.

Should work awesomely, especially on something low-poly like an isosphere, although on a UVsphere may slow, but shouldn’t too much…

Works fine if I use one box and one ball but a simple level in my game has 16x8 boxes, and the ball has 16 vertices now. On every frame I have to check collision 2048 times.

I might have implemented it wrong but I don’t have the script anymore since I modified it to work on my own setup. I plan to finish my setup tomorrow, its pretty fast and fairly precise if the speed of the ball is not to high.

Raycasts will only go from the center outwards. To check the bounds of the sphere, use two - one on each side of the ball checking forwards on the angle the ball is moving (as well as a center one). Also, why not just subtract the position of the hit object (brick) from the position of the ball to get the position of collision? You can also figure out the angle from the ball to the brick with the object positions as well.

subtracting the positions would give the difference between the two, (duh) but in the form of a co-ordinates which are relative to the origin (0,0,0), for example, if you spawned an object at the point calculated by the subtraction, you’d get an object spawning the difference between the vectors, but away from [0,0,0].

You could, however, raycast from the ball to the brick and get the hit position…(Raycast API)
Raycast is the last item on the list.

I have no idea why i didn’t think of it earlier facepalm

Guys I think you are making this way too complicated…why don’t you just use the bullet physics capabilities.
Look at this example I made for 12 minutes.
No Python, No Logic…just bullet physics.

/Hint: Examine the material properties and the collision bound of the objects/
Good luck!

@haker - I am aware of the fact that subtracting positions would give you a point basically emanating from point 0, 0, 0. I thought that was what you wanted. In any case, a raycast from the ball to the collided brick would give you the angle.
@haidme - Yes, the Bullet physics may be more accurate. Set the gravity to 0 and restitution up and the ball should bounce around accurately.

Thank you all.