playMaker

Author Topic: 2D Reflection help  (Read 1526 times)

TheNecron

  • Playmaker Newbie
  • *
  • Posts: 17
2D Reflection help
« on: March 03, 2020, 01:40:00 AM »
So I'm trying to create an effect where a projectile reflects off of walls using the normal of the the collision hit point. I have gotten so far as to use Ray Cast 2d to store the normal hit point in a Vector2 (as 0.0, -1.0 for example) but I'm having difficulty understanding how to use the Vector2 normal to actually set the rotation of my projectile appropriately. There's not an exact set direction action and I'm not sure Transform Direction is what I'm looking for but its very possible I'm just missing something in terms of how to input variables or something. Any explanation would be very helpful, thanks.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2D Reflection help
« Reply #1 on: March 03, 2020, 02:05:21 AM »
Hi,

The raycast normal is always expressed in world coordinates, you need to use the action GetQuaternionFromRotation to get a transform rotation that matches the contact normal for a given object.

Quote
transform.rotation = Quaternion.FromToRotation(transform.up, normal) * transform.rotation;

so first get the fromToRotation using the transform up as the from direction and the contact normal as the normal, then multiply by the rotation. that will be your new rotation matching the normal direction.

 Let me know how it goes.

Bye,

 Jean

TheNecron

  • Playmaker Newbie
  • *
  • Posts: 17
Re: 2D Reflection help
« Reply #2 on: March 03, 2020, 05:00:29 PM »
I did GetQuaternionFromRotation From the direction of the initial raycast that got the normal To the normal and stored that quaternion (example log result 0,180,180) which i believe is correct. I'm somewhat uncertain on the next part though, i tried GetQuaternionMultipliedByQuaternion using the quaternion that was just calculated from the rotation multiplied by the quaternion variable that i stored for the objects initial rotation and then setting the rotation to that final quaternion.

It seems to calculate properly whenever it hits flat edges moving upwards regardless of angle but whenever it hits flat edges moving downwards it doesnt rotate at all and just pierces through the wall, whenever it hits moving left it always rotates 90 degrees clockwise, and whenever it hits moving right it always rotates 90 degrees counterclockwise.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2D Reflection help
« Reply #3 on: March 06, 2020, 03:52:45 AM »
Hi,

 you have likely a problem of space ( Local vs global)

 make sure you express the normal in world space.

Let me know if you still can't figure it out, I'll do a quick sample to demonstrate that if needed.

Bye,

 Jean

TheNecron

  • Playmaker Newbie
  • *
  • Posts: 17
Re: 2D Reflection help
« Reply #4 on: March 18, 2020, 07:02:40 PM »
Using the Ray Cast 2d function the Store Hit Normal does not appear to have an option for world/self space, I am using self space for the direction of the raycast because i thought that was easier, is the function inheriting that when expressing the result normal?

In any case, a sample would be appreciated

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2D Reflection help
« Reply #5 on: March 20, 2020, 04:11:24 AM »
Hi,

 the normal is always expressed in world space. Can you try with this in mind?

Bye,

 Jean

TheNecron

  • Playmaker Newbie
  • *
  • Posts: 17
Re: 2D Reflection help
« Reply #6 on: March 20, 2020, 09:54:45 PM »
I'm not sure I understand that response. If you're saying the normal is being expressed in world space properly then there are no other space options besides on the Get/Set Rotations which are using world space. Is there something else I need to be trying?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2D Reflection help
« Reply #7 on: March 25, 2020, 05:15:48 AM »
Hi,

 yes there is, you can express a direction or a rotation in local space as well. so when you get a normal in world space, and want to deduce the direction an object should aim at in order to get along that normal, you will have to make sure all your math is properly reference rotation and directions in world space, many actions gives you the option to choose between world space and local space.

Bye,

 Jean