PDA

View Full Version : Collisions in webgl. Can you do it with a shader?



jimmyt1988
03-06-2012, 08:26 AM
Hi all, excuse my pseudo code.

Can you confirm my method for determining collisions.

I have given my player model a bounding cube (so i dont have to check all the humps and bumps colliding, rather just one plane).

I know that the plane that faces the direction im walking can be represented by 3 vertices:



var frontPlane = [p1, p2, p3] //gives a way of measuring distance from player to object points
var normalOfFrontPlane = [v1x, v1y, v1z] //gives a direction of that plane


I know that the current object (objects that are not my player) I'm iterating through has many normals/planes.

If I find the dot product (cosine) between my players normalFrontPlane and all the normals on the object im currently checking.


if ( cos <= 90 )

then find the distance between any 2 points on that plane (including the vertices that create the plane itself) -


sqrt((x2-x1) + (y2-y1) + (z2-z1))

if that distance is <= playerMaxCloseness, then movement stops.

Does that sound right? If so.. great. tell me, coz im unsure.
Second question is, is there a better way to do all this in the shader file?
It seems like this operation would be quite slow?

jimmyt1988
03-06-2012, 08:42 AM
Too late to edit but.. < 90, not <= 90. thatd be dumb :D

jimmyt1988
03-07-2012, 03:20 PM
I did a simple sqrt(x + y + z). thats from player origin to each point on all the objects and if the distance is > 2, then move.

But it's so flimsy.

If i have a very long wall.. i need to know if i hit into that too.. so im gnna have to "cast a ray" from my player and interpolate the wall.. but surely thats gnna be slow.. is there a way of doing this shiz in the shader?