playMaker

Author Topic: Reflecting bullet FSM  (Read 3006 times)

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Reflecting bullet FSM
« on: June 24, 2016, 04:25:18 AM »
Hello all!

I am trying to make a bullet reflect off a collider in 2D game, but i just can't get my head around it. I checked out the breakout sample game, but that's not quite it since it has a fixed reflection angle based on impacting left or right collider of the paddle and the brick, as far as i have seen. There are some pinball playmaker games, but i think there's also no reflection there, just adding force to the ball when hitting the flipper. I googled a lot, and even found some scripts, but nothing does the job unfortunately. I think it can be done fairly simple in Playmaker, but i need some help.

The bullet has a fixed velocity (not force), so upon colliding it only needs to change direction whle keeping the velocity. I am aware i need to use vector3 reflect under the operator advanced action and i've read about the normal of the impact and calculation of the exit angle and thus the rotation that needs to be applied to the bullet but don't quite know how to implement it.

Any help or example is welcome!

cwmanley

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Reflecting bullet FSM
« Reply #1 on: June 24, 2016, 09:07:16 PM »
Velocity (X,Y) -1 , will reverse the direction.

GetVelocity2d/Math/SetVelocity2d

 :)

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #2 on: June 25, 2016, 12:35:34 PM »
Yes, but what should be the math part ;D

cwmanley

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Reflecting bullet FSM
« Reply #3 on: June 25, 2016, 01:10:53 PM »
The Math is " Velocity (X,Y) -1 "


 :)

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #4 on: June 26, 2016, 02:00:11 AM »
Works good, the thing is, how to i set it to reflect under angle when colliding with circle collider?
« Last Edit: June 26, 2016, 02:03:49 AM by krmko »

cwmanley

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Reflecting bullet FSM
« Reply #5 on: June 26, 2016, 11:22:38 AM »
Sorry, I did not think it all the way through. This should work.


krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #6 on: June 27, 2016, 03:43:37 AM »
Thank you,

you are very kind, i will try it out  ;D

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #7 on: June 27, 2016, 05:14:43 AM »
Hmm, the thing is, the object that's being hit actually ricochets under angle, but the object that should ricochet stops when it hits the target :-\

rongconcrx

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Reflecting bullet FSM
« Reply #8 on: June 27, 2016, 08:26:41 AM »
I'm also having trouble figuring this out, My issue with this fsm is when my bullet hit a surface, it will bounce back only either on the x or y axis.

It doesn't go in the angle even the bullet is traveling at an angle.

cwmanley

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Reflecting bullet FSM
« Reply #9 on: June 27, 2016, 12:39:27 PM »
Thanks,

This is a little better, but the "bullet" can stop if it makes constant contact with something like a object moving in the direction it is traveling. 

Changing layers after the collision might be solution.


rongconcrx

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Reflecting bullet FSM
« Reply #10 on: June 27, 2016, 01:57:05 PM »
Awesome! Thank you!

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #11 on: June 27, 2016, 11:07:08 PM »
have you managed to get it working rongconcrx? My bullet just hits the ball and stops.
« Last Edit: June 27, 2016, 11:34:57 PM by krmko »

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #12 on: June 28, 2016, 02:46:44 AM »
I've found an elegant solution. Since i need the reflection to ricochet the bullets off of shield of an enemy, i can simply set the bouncy physics material to shield and set bounciness to 1 so the bullet doesn't lose power and flip the sprite.

Expansion of the idea would be even better, a shield could change the physics material according to the tag of the bullet, so some bullets would pass, some would reflect, depending of the type of the bullet. Alas, there's no action for setting the physics material either :(
« Last Edit: June 29, 2016, 07:10:25 AM by krmko »

cwmanley

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Reflecting bullet FSM
« Reply #13 on: June 30, 2016, 06:56:28 PM »
I am sorry my solution did not work, but I am glad you found a solution you like.

If your still having trouble, I am glad to help as much as I can.

Thanks
« Last Edit: June 30, 2016, 07:07:12 PM by cwmanley »

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Reflecting bullet FSM
« Reply #14 on: July 05, 2016, 02:21:20 AM »
Hey, sorry gor late response, i've been quite busy. Thanks a lot for the help, you really gave yourself there, it gave me insight in how things work and i'm sure i'll make it work somehow and share the template with everyone. Until then, Jean made set physics material 2d action and the sprite can be flipped via action so making bullets bounce now is a no brainer!