Posted by Fobwashed (2532 posts) -

This is a short vid, with a long post about how I dealt with the gravity situation. I love physics. I love math. But they can be a real pain in the ass when you don't understand them -_-;;

Loading Video...

The Math Behind Gravity

So, I've had a pretty basic way to do targeting before with the balls that show the trajectory and whatnot, but it was janky and a sort of slamshod put together business and was pretty restrictive in the types of aiming I could use. The hardest restriction being that the height of the throw couldn't be altered on the fly =\ This had to be changed so I delved into the math behind gravity and how I could use that to determine arcs and such. The following is basically a math class for anyone interested. To some it'll be sort of hard math, to most it's basic physics with some intermediate algebra. All I know is, I remember when I was learning this stuff in school thinking "when the hell am I ever gonna need this crap? Quadratic formula? psshhhhhhh" but lo and behold, this shit's everywhere.

Basic Physics

Physics wise, you can pretty much completely separate directional movement and the force of gravity. At least, in this game you can as I'm not going to calculate anything like wind resistance and whatnot. . . at least not yet. Getting the direction that an object should travel is easy. You just normalize the difference between the target and the object tossing the item. The problem is determining how fast this object should travel at a given launch velocity. This gets even more complicated (at least for me) when you start to factor in varying heights for the target and the tosser.

The Formulas

The base of applying gravity is simple. The speed an object is moving vertically is just increased by the amount of gravity over and over. This is simplifying things greatly if you're looking at the real world, but again, I'm working without any frictional drag. And also as of yet, haven't found the need to apply any sort of cap on fall speed. So, if I want to figure out the Y value of an object given a launch speed and an elapsed amount of time,

Y = launch speed * elapsed time - gravity * elapsed time ^ 2 * 0.5.

That right there is how I determine how high the targeting balls are to represent the arc the thrown object will take.

Now, if I'm talking just plain old flat ground, the speed that the object will travel is easy to find out. Basically, using the previous formulas, you can determine how long an object will be in the air assuming it was thrown from the ground, and will land on the ground. You can use an easy

time of flight = 2 * launch speed / gravity

Knowing how long something is in the air, you simply divide the distance between the two points and you know how fast you need to travel to reach the destination as the object is hitting the ground.

The real difficulty I ran into was figuring out the arc to hit the target when the target was above or below the tossing point. To get this bit of information, I ended up using the quadratic formula. First, I had to figure out if the target is above or below me using both their heights and getting the difference. If I always place the tosser at (0,0), I could use the QF, plugging in the difference in height as the Y value to figure out how much time, with a set launch velocity, would have to expire before the tossed object hit that height.

Time In Air Till Impact = launch velocity / gravity + sqrRoot ( launch velocity ^ 2 / gravity ^ 2 - 2 * target height / gravity)

With the knowledge of when in time, an object launched at a specific velocity would hit the ground at the specified height, I can now divide the distance that I need to cross by that amount of time and get the speed that the object needs to be throw in. And BAM.

Conclusion

I've got the core of all my gravity based targeting in place. It's set up in a way in which I can vary the height and if I wanted to, the speed of thrown objects. This'll go a long way in terms of enemy attacks in the future as well as giving the player more options. I'll still have to figure out a nice way to give the player control over their item throws, while balancing the complexity of the action. Progress.

#1 Posted by Fobwashed (2532 posts) -

This is a short vid, with a long post about how I dealt with the gravity situation. I love physics. I love math. But they can be a real pain in the ass when you don't understand them -_-;;

Loading Video...

The Math Behind Gravity

So, I've had a pretty basic way to do targeting before with the balls that show the trajectory and whatnot, but it was janky and a sort of slamshod put together business and was pretty restrictive in the types of aiming I could use. The hardest restriction being that the height of the throw couldn't be altered on the fly =\ This had to be changed so I delved into the math behind gravity and how I could use that to determine arcs and such. The following is basically a math class for anyone interested. To some it'll be sort of hard math, to most it's basic physics with some intermediate algebra. All I know is, I remember when I was learning this stuff in school thinking "when the hell am I ever gonna need this crap? Quadratic formula? psshhhhhhh" but lo and behold, this shit's everywhere.

Basic Physics

Physics wise, you can pretty much completely separate directional movement and the force of gravity. At least, in this game you can as I'm not going to calculate anything like wind resistance and whatnot. . . at least not yet. Getting the direction that an object should travel is easy. You just normalize the difference between the target and the object tossing the item. The problem is determining how fast this object should travel at a given launch velocity. This gets even more complicated (at least for me) when you start to factor in varying heights for the target and the tosser.

The Formulas

The base of applying gravity is simple. The speed an object is moving vertically is just increased by the amount of gravity over and over. This is simplifying things greatly if you're looking at the real world, but again, I'm working without any frictional drag. And also as of yet, haven't found the need to apply any sort of cap on fall speed. So, if I want to figure out the Y value of an object given a launch speed and an elapsed amount of time,

Y = launch speed * elapsed time - gravity * elapsed time ^ 2 * 0.5.

That right there is how I determine how high the targeting balls are to represent the arc the thrown object will take.

Now, if I'm talking just plain old flat ground, the speed that the object will travel is easy to find out. Basically, using the previous formulas, you can determine how long an object will be in the air assuming it was thrown from the ground, and will land on the ground. You can use an easy

time of flight = 2 * launch speed / gravity

Knowing how long something is in the air, you simply divide the distance between the two points and you know how fast you need to travel to reach the destination as the object is hitting the ground.

The real difficulty I ran into was figuring out the arc to hit the target when the target was above or below the tossing point. To get this bit of information, I ended up using the quadratic formula. First, I had to figure out if the target is above or below me using both their heights and getting the difference. If I always place the tosser at (0,0), I could use the QF, plugging in the difference in height as the Y value to figure out how much time, with a set launch velocity, would have to expire before the tossed object hit that height.

Time In Air Till Impact = launch velocity / gravity + sqrRoot ( launch velocity ^ 2 / gravity ^ 2 - 2 * target height / gravity)

With the knowledge of when in time, an object launched at a specific velocity would hit the ground at the specified height, I can now divide the distance that I need to cross by that amount of time and get the speed that the object needs to be throw in. And BAM.

Conclusion

I've got the core of all my gravity based targeting in place. It's set up in a way in which I can vary the height and if I wanted to, the speed of thrown objects. This'll go a long way in terms of enemy attacks in the future as well as giving the player more options. I'll still have to figure out a nice way to give the player control over their item throws, while balancing the complexity of the action. Progress.

#2 Posted by Rudyftw (555 posts) -

.......Did you ever stop to think that maybe only communists like physics.

#3 Posted by Fobwashed (2532 posts) -

@Rudyftw: It did cross my mind. I always had my suspicions about Havok.

#4 Posted by Rudyftw (555 posts) -

@Fobwashed said:

@Rudyftw: It did cross my mind. I always had my suspicions about Havok.

Who is this "Havok"?

Is he.... is he me?

#5 Posted by Fobwashed (2532 posts) -
#6 Posted by JohnnyMcmillen (156 posts) -

This was a pretty interesting read as someone just about to do a physics degree, I figure you've got this figured out now, but it got me thinking of some ways how do this in different ways.

#7 Posted by Fobwashed (2532 posts) -

@JohnnyMcmillen: I'm always open to hearing ideas =) Especially if they're better than what I've cobbled together. For the most part, the way I've got it implemented serves it's purpose but if you've got something better, fire away! As it is, there's more math in this than I'd like but since it's only doing the calculations when I bring up the targeting cursor, I'm not too afraid that it'll take up too many resources processor wise but anything cheaper is always better too as long as it can get the job done.

#8 Posted by EightBitShik (1448 posts) -

Is that Abe Lincoln? do you get to sleep with a bunch of girl in this game?

#9 Posted by I_smell (4212 posts) -

Y'know how I do gravity in games?
 
every frame {
_y + fallspeed
fallspeed + 1
}
 
I would've drawn the arc just by having the game simulate all the positions of a ball throw in one frame, and having it draw a line through those.
...but then I read it again and I guess that's what you did?

#10 Posted by Fobwashed (2532 posts) -

@I_smell: That's basically how I handle all gravity in my game as well. Everything has it's own current fall speed and gravity adds to it every update until it hits the maximum speed. With the exception of when time slows at which point I multiply the gravity amount by the slow percentage.

As for the arc, the easiest way would prolly have been to do what you mentioned. Just simulate in one update every position that the tossed item would go through during it's entire flight time but I figured that'd be too expensive. Or at least, more expensive than calculating a set number of points during the arc.

One item, in the air for say 4 seconds would be 4* 60 position updates so 240 position calculations per update while the targeting cursor is active. That's fairly cheap, but then I'd also have to calculate collision tests for every one of those updates to account for going up and down platforms to factor in the height that the item would fall or rise above and that would vary per level but say it checked against a low count of say 20 collision checks to determine if it was clearing platforms. The checks then jump to around 4800 per update. Those checks also involve various things in and of themselves so it does sorta add up.

I needed to work out the formulas I used anyway so that I could get the proper velocity to throw an item at to reach the destination taking into account the rise or drop in height. The player has control over the height the item gets thrown in addition to the target landing position so with those I needed to come up with the ground speed anyway. I dunno. . . I'd really have to think it through but I think plugging in specific points along the trajectory by multiplying the direction by the distance I want to check the height of the item at and then applying stupid physics is cheaper. We're not talking huge gains but every little bit helps, especially when dealing with the xbox -_-;;

Drawing a line through the points might be better than balls too. . . I'm sure I'll come back to this when I'm redoing the visuals for the cursor =P

#11 Posted by I_smell (4212 posts) -
@Fobwashed said:

@I_smell:

One item, in the air for say 4 seconds would be 4* 60 position updates so 240 position calculations per update while the targeting cursor is active.

Well if it's 60fps then you could easily have it just simulate every 3rd frame- and in this video the ball's only ever in the air for about 2 seconds anyway.
So there, I just cut your 240 calculations down to... 40.
I mean; when you consider how ray-tracing bullets work, this isn't "expensive" at all. If I can have like 10 different line-of-sight beams tracin around in a browser game, I'm pretty sure you can simulate a bouncing ball on an Xbox without anyone noticing.
 
BUT-- I come from the school of "just do what works", so faulting you for actually thinking about it is probably pretty dumb.
 
The absolute best simulated-throw stuff is in Gears of war with the grenades and the Torque Bow.
#12 Posted by Fobwashed (2532 posts) -

@I_smell: I hear yah. I'm still new to this whole thing and I have no clue how much I can push before the 360 begins to stress so from the outset I've been trying to keep things lean if possible. A while back I threw on as many mindless potatoes as I could to see how many I could get on before it started slowing and I capped out at around 80~90. While I'll prob never need that many enemies on screen, it was a bit disappointing that even with no real AI and not that much going on, the system could only handle so few.

As for the browser game line of sight beams, I don't know what goes into that stuff but from how you're puttin it, I gotta assume they're pretty complicated. Though, I'm not sure that stipulating that it's for a browser game doesn't really mean much since I doubt that browsers cap the cpu's calculations so it's pretty much a PC game.

I always start with the "just do what works" as well, but as soon as I get something functioning, I'll continue to rework it till it's as good as I can make it (which I admit, is prolly not all that great). I've heard multiple times that I should just press on and only optimize when it becomes necessary but it's some kind of stupid disorder I've got that forces me to try to clean everything I can up =\

I can't think of any other better uses of simulated throwing but GoW had some pretty good stuff. The only other thing that comes to mind is just straight up dropping in Peggle. . . I played a lot of peggle =P

#13 Posted by DarkShaper (1362 posts) -

The more I see of this game the more I want it to only take place in one clearing in the middle of a forest.

#14 Posted by YOUNGLINK (581 posts) -

sweet game, you got my support homie