Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: magritte on July 21, 2014, 10:59:10 PM

Title: 2D bounce off of surface
Post by: magritte on July 21, 2014, 10:59:10 PM
I'm trying to do the same thing this guy wants to do:

Here's what I want to do.

(https://dl.dropboxusercontent.com/u/62133801/Vector2_doubt.png)

I don't understand which Vector2 actions i should use to:
1) get direction from arrow.
2) during collision, reflect on the normal vector of the barrier.
3) give arrow new direction.

http://hutonggames.com/playmakerforum/index.php?topic=5992.msg29092#msg29092 (http://hutonggames.com/playmakerforum/index.php?topic=5992.msg29092#msg29092)

However, the proposed script doesn't work exactly right. The arrow does bounce off of the walls in the proper direction, but the arrow does not then turn to face the direction of reflection as specified in the drawing. Instead it kinda spins around according to the physics simulation.

What I would like to do is set the velocity (with the set velocity 2d action) of the arrow for the approach, detect the collision with the wall, and then calculate the reflection direction based on the surface normal of the wall. The problem is I can't figure out how to get the wall's surface normal. Using raycast checks and collision checks, all I can find is the normal of the collision point -- and since the arrow faces the collision point directly, the collision normal just points right back at the arrow.

So, does anybody know how I can find the actual surface normal, in world space, of the wall the arrow collides with? Or perhaps an alternative method to accomplish the same goal?

Thanks for any assistance.
Title: Re: 2D bounce off of surface
Post by: jeanfabre on July 22, 2014, 04:48:08 AM
Hi,

 the easiest approach here is to constraint the direction of your object to be the same as it's velocity.

1: use "Get velocity 2d" action, record the velocity as a vector2
2: use SmoothLookAtDirection using the velocity above as the "Target Direction" ( you'll need to inject the velocity into a vector3  first)

You can see a working sample ( in 3d, but the principle is identical for 2d) here with a missile being fired, and the missile object aims automatically where it feels natural ( pointing at where it's going).

http://hutonggames.com/playmakerforum/index.php?topic=4477.msg25721#msg25721

Bye,

 Jean
Title: Re: 2D bounce off of surface
Post by: magritte on July 22, 2014, 09:29:17 AM
Thanks, I will give this a try as soon as possible.

As a general question, do you know of any method for obtaining the normal vector of a surface?
Title: Re: 2D bounce off of surface
Post by: jeanfabre on July 30, 2014, 06:02:32 AM
Hi,

 the normal vector of the surface is obtained using the hit info, it's the "normal" vector.

 now if you want the know what would be the direction of the ray if it was to bounce off this surface, you would need to use the action "Vector3Operator" with the operation "reflect", passing the current direction against the surface normal.

 Also, there is another thread on this, have you noticed the solution provided? did you tried it?

http://hutonggames.com/playmakerforum/index.php?topic=5992.0

Bye,

 Jean