Staredit Network > Forums > SC2 Assistance > Topic: Physics - Bouncing logical at edges
Physics - Bouncing logical at edges
May 6 2010, 1:33 pm
By: Devourer  

May 6 2010, 1:33 pm Devourer Post #1

Hello

Heya, I need your support once again^^
How do I let several units bounce at edges, just like a ball hits a wall: it goes away of it in the angle like it was shoot towards (other side, I dunno how to express, but I think you guys are knowing what I mean)?
Also, can this cause lag?



Please report errors in the Staredit.Network forum.

May 6 2010, 4:00 pm CecilSunkure Post #2



When an object hits the either the left or right boundary you reverse the x velocity, and when the object hits a top or bottom boundary you reverse the y velocity. When the boundaries aren't vertical or horizontal you have to use a specific ratio of modification to both the x and y velocity vectors.

By reverse I mean multiply by -1.



None.

May 6 2010, 4:02 pm Devourer Post #3

Hello

Well I have walls... many walls, all directions (in fact, wall and low ground).
How to do it then?

Examples and deep explaining would be soooo grateful :(

Post has been edited 1 time(s), last time on May 6 2010, 4:48 pm by DeVouReR.



Please report errors in the Staredit.Network forum.

May 6 2010, 4:57 pm samsizzle Post #4



you're gonna need a lethal_illusion.



None.

May 6 2010, 5:14 pm CecilSunkure Post #5



Well I haven't been able to use SC2 at all, so I don't know how to specifically do this in the SC2 editor.

If you have a 45 degree angled wall, you switch the x and y velocities.

For things other than it gets a bit more complicated. Here is one method:

Imagine your wall being intersected by a horizontal line. The smallest angle formed by this intersection is represented by A. When a ball hits an angled surface, you modify the angles of the velocities. You modify the x velocity by setting it equal to pie/2 + A. You set the angle of the y vector to pie/2 - A. Your new vectors will represent the velocities of the object after it hits the wall, however, they aren't in a form that can easily be interpreted as x and y velocities, they are now in perpendicular and parallel velocities (to the wall). So what you do is convert the parallel and perpendicular into new x and y velocity vectors by plugging a few variables into an equation.

For simplicity's sake, the parallel = f(Vxcos(A) + Vysin(A)). The perpendicular = -e(Vycos(A) - Vxsin(A)).

So now you need to modify these two velocities back into x and y representations.

Here are the equations for the final velocities of x and y.

New Vx = Vx(fcos2A - esin2A) + VysinAcosA(f + e)
New Vy = VxsinAcosA(e + f) - Vy(ecos2A - fsin2A)

You can plug A and the x and y velocities into these two equations to solve for the new velocity vectors after a collision with a wall, in which the A angle represents the smallest angle formed by an intersection between the wall and an imaginary horizontal line.

Here is my source: http://www.newgrounds.com/portal/view/448915

The only thing I didn't understand was what f and e were. I'm assuming e is Euler's number, and I'm not sure what f is. You should probably get someone else in here like Lethal.

Also, there is a really simplified notation out there that uses the dot product along with the perpendicular and normal velocity, and the velocity of the object. This is what I myself would use, but I don't want to explain how to use the dot-product without having matrix representation.

This is basically the exact same for three dimensions, just with an extra velocity vector z. If you want to have collision between two balls, then you need to calculate the slope of the line formed between the center of each circle. This is easy, rise over run. To get the rise over run you use the Pythagorean theorem (also known as the distance formula) on both of the center points from each circle. Once you get the slope, reverse the numerator and denominator to get the perpendicular, then do the entire above process using this perpendicular slope as your "wall" to collide with.

Post has been edited 1 time(s), last time on May 6 2010, 5:21 pm by CecilSunkure.



None.

May 7 2010, 3:03 pm CecilSunkure Post #6



Yep, I just figured it out. f is friction and e is elasticity. So, they are constants you set within your code. Usually e and f are between 1 and 0. 0 means the ball stops whenever it hits something, and 1 means perfect elasticity. There is a much simpler formula to use if both e and f equal one another, where they are substituted with i. This equation can be used only if your elasticity and friction are equal and constant, and this equation is more efficient than the last:

final Vx = Vxcos(2A) + Vysin(2A)
final Vy = Vxsin(2A) - Vycos(2A)

If anyone would like me to explain the math more in depth I can, actually, I should probably write a wiki on this. The only downside is that I can't use the editor, so I can't explain how to implement this into a SC2 map :P

We'll have to wait until I get my new laptop for that.



None.

May 8 2010, 4:22 am CecilSunkure Post #7



I talked to Lethal and now we have some even simpler equations. This is assuming the collision is elastic (objects don't change speed during the collision). First you solve for the new angle the object will be travelling in after the collision.

A = Angle the object is travelling before impact (angle of incidence).
B = Angle the object is travelling after impact (resulting angle).
C = Normal angle to the wall. -This the angle of a line perpendicular to the wall at the point of impact. For a vertical wall, this would be 180.

The equation is 180 + 2C - A = B.

Here is an example:

Say an object is moving right. This means it's angle of incidence is 0. This object then collides with a wall that is sitting at a 45 degree angle.



So now, plug your info into the equation 180 + 2C - A = B.

180 + 2(135) - 360 = 90

Your resulting angle is 90 degrees, which is straight up as the diagram shows.

Now using this angle you need to modify the x and y position of the object using these equations:

Velx = mag * cos(B)
Vely = mag * sin(B)

Note: You can substitute in tan-1(y/x) for A if you know the slope, which is y/x.

The magnitude (mag) is a constant that you set. If you want your objects to move at 5 units per second, use a magnitude of 5. Then, modify the object's current x and y coordinates by the Velx and Vely values

You could also use these two equations if you know the previous velocity (which would be the magnitude*x or y component from the previous equations):

final Vx = Vxcos(2A) + Vysin(2A)
final Vy = Vxsin(2A) - Vycos(2A)

Post has been edited 2 time(s), last time on May 8 2010, 11:32 pm by CecilSunkure.



None.

May 8 2010, 4:34 am Centreri Post #8

Relatively ancient and inactive

Cecil: Physics is easy assuming you have all the information. The hard part is getting the information. There's no function for getting the slope under the ground, and there isn't a function for defining the equation for the terrain surface at a given point. What the hell are you doing...

Dev, are you using regular cliffs or terrain manipulation, as in raising/lowering/plateauing/etc it to create terrain?



None.

May 8 2010, 4:37 am CecilSunkure Post #9



This would make a unit that hits a wall bounce off and keep going, and works for any angle of wall or unit travel. So this is just 2 dimensional, and isn't taking into account the z coordinate for height.

I don't think you even need to mess with the x and y velocity vectors of the unit with SC2, but since I don't have the editor I'm just describing the entire process mathematically.



None.

May 8 2010, 4:39 am Centreri Post #10

Relatively ancient and inactive

Stop describing the process mathematically. It's reflection over across the tangent to the surface, it takes one line to explain...

Shoo, this is for people with the editor. Throwing equations at Devourer is about as useful as hopping on one foot.

Devilesk already told you, but just so you can look at it again, the "Reflected Point" function is perfect, assuming you find the wall angle. One way to do it is to create regions at each specific wall type, and return the value if it hits the walls there. I still can't find a way to specifically find the wall type, though.

Also, novel solution: Assuming you're using cliffs, which can only face eight directions, you can find the heights at maybe eight points at even angles and distances away from the contact point and use that information to determine angle. For example, if the bottom four are at 0 and top four at one, it's a horizontal cliff and you reflect across 0 (or however Reflected Point works). Etc. I'd write a separate function to handle this, because you may find it useful later. This technique can also be used to APPROXIMATE the angle of hills (as opposed to cliffs), if you want to do that. The more points, the greater accuracy. It can be frustrating, but damn, it'll work. :P

Post has been edited 3 time(s), last time on May 8 2010, 5:13 am by Centreri.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[09:38 pm]
NudeRaider -- Ultraviolet
Ultraviolet shouted: NudeRaider sing it brother
trust me, you don't wanna hear that. I defer that to the pros.
[2024-4-27. : 7:56 pm]
Ultraviolet -- NudeRaider
NudeRaider shouted: "War nie wirklich weg" 🎵
sing it brother
[2024-4-27. : 6:24 pm]
NudeRaider -- "War nie wirklich weg" 🎵
[2024-4-27. : 3:33 pm]
O)FaRTy1billion[MM] -- o sen is back
[2024-4-27. : 1:53 am]
Ultraviolet -- :lol:
[2024-4-26. : 6:51 pm]
Vrael -- It is, and I could definitely use a company with a commitment to flexibility, quality, and customer satisfaction to provide effective solutions to dampness and humidity in my urban environment.
[2024-4-26. : 6:50 pm]
NudeRaider -- Vrael
Vrael shouted: Idk, I was looking more for a dehumidifer company which maybe stands out as a beacon of relief amidst damp and unpredictable climates of bustling metropolises. Not sure Amazon qualifies
sounds like moisture control is often a pressing concern in your city
[2024-4-26. : 6:50 pm]
Vrael -- Maybe here on the StarEdit Network I could look through the Forums for some Introductions to people who care about the Topics of Dehumidifiers and Carpet Cleaning?
[2024-4-26. : 6:49 pm]
Vrael -- Perhaps even here I on the StarEdit Network I could look for some Introductions.
[2024-4-26. : 6:48 pm]
Vrael -- On this Topic, I could definitely use some Introductions.
Please log in to shout.


Members Online: NudeRaider