# Particle bouncing off wall. [SOLVED]

On 24/01/2015 at 04:34, xxxxxxxx wrote:

Hi
I'm about to start a project which involves moving particles that bounce off or flow along a geometric surface.
As a starting test, I'd like to bounce a particle around inside a cube.  So the particle has a position and a random starting velocity, and when it reaches a wall of the cube, it bounces in the appropriate direction. Looking around the SDK I can't see much in the way of collision detection and physics, so I guess I'll have to work things out manually - which I prefer anyway.

So my particle needs to know if it has reached a polygon (wall of the cube) - how might this be this done?
I would then imagine a reflection angle needs to be calculated based on the velocity vector of the particle and the normal of the polygon.

Any help appreciated, cheers,
Glenn.

On 24/01/2015 at 09:47, xxxxxxxx wrote:

Take a look at GeRayCollider for your collisions. Then yes, once collided you'll have to calculate the new direction. That's simple vector maths, which you will use a *lot* if you start moving particles around.

Steve

On 24/01/2015 at 09:57, xxxxxxxx wrote:

ahhh perfect thank you!

On 25/01/2015 at 03:49, xxxxxxxx wrote:

Hi again
I got a basic test working which is great - I just have one small question.  When I move the source position of the ray, the collision point changes as expected, however when I move the collision object around nothing changes.

import c4d
from c4d.utils import GeRayCollider

def main() :
sourceobj = doc.SearchObject("Null") # The object the ray will start from
targetobj = doc.SearchObject("Cube") # The object the ray collides with
ray = GeRayCollider()                # Create a new GeRayCollider object
ray.Init(targetobj, True)            # Assign the object to a variable

start = sourceobj.GetAbsPos()
direction = c4d.Vector(0, 1, 0)      #The direction the ray points.

distance = 1000

CollisionState = ray.Intersect(start, direction, distance)
if CollisionState: targetobj[c4d.ID_BASEOBJECT_XRAY] = True
else:  targetobj[c4d.ID_BASEOBJECT_XRAY] = False

intersection = ray.GetIntersection(0)["hitpos"]
print intersection

main()

On 26/01/2015 at 13:15, xxxxxxxx wrote:

Hi glenn,

Nothing changes when you move the collision object around because you're likely changing its world coordinates, not its object coordinates, and yet are doing the ray collision calculations from object space.  Your sourceobj.GetAbsPos() call, for instance, is in object space.  When you move the source position of the ray, it changes in object space, hence the collision point changes occur as expected.

If you really need it to be in world space, here's a link to a thread pointed out to me by Andreas that could help:

Position/Normal under Cursor

I hope that helps!

Joey Gaspe
SDK Support Engineer

On 26/01/2015 at 14:29, xxxxxxxx wrote:

Thanks very much Joey for the explanation.   It's not something urgent I need working, but might want to look at in the future.  I'll use your link for reference.

A quick work around I found is if I make the Null a child of the Cube - and move the cube around, then offset of the Null back to where it was, then that seems to work.

Cheers,
Glenn.

On 26/01/2015 at 14:36, xxxxxxxx wrote:

Hi Glenn,

Thanks for letting me know.  Your use of the Null for your Cube is certainly a workaround that should work without issue.  I'll set this topic to solved, but leave it open for comments.

Thanks,

Joey Gaspe
SDK Support Engineer