Unit bouncing back from walls?

WayTooShort

New Member
Reaction score
4
How can I make unit bounce from a wall to the right direction? I basically got this dummy unit which is moved by periodical triggers towards a target location, but if it meets a wall in the way (unpathable terrain) I want it to bounce back from it.

What I tried was checking the angle between start location of the unit and the position of the unpathable terrain. Look at the picture:

(http://yfrog.com/71prochartj if the thumbnail doesn't work)

The angle between those two points is same in both examples (a and b), but the unit should bounce to different direction. What should I do?
 

Attachments

  • prochart.JPG
    prochart.JPG
    8.3 KB · Views: 144

uberfoop

~=Admiral Stukov=~
Reaction score
177
If all of your walls are either straight verticle, or straight horizontal, you can just reverse the sign of one component of motion. For example, if a unit strikes a horizontally-running wall such as the bottom right one in your diagram, you can just flip the sign on the unit's y-dir velocity.

//====================

More generally, suppose that a wall points at an angle A (I'm not talking normal angles here, I'm talking the direction the wall itself points as a line), and suppose that a particle's velocity is at some angle B. Then the reflection angle C is:
C=2A-B

For example, imagine a wall in a 45 degree angle and suppose a unit is moving in the 180-degree direction. Then, it should reflect at C=2*45-180 = -90.

This works in any way of defining angles (whether it be degrees or radians or whatever) as long as you're consistant. Furthermore, it works even if your angles are out of the normal 360-degree range (ie 500 degrees).
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top