1. Math question for game...
- Posted by Robert B Pilkington <bpilkington at JUNO.COM> Sep 12, 1997
- 831 views
Hello everybody....... Right now, I'm working on an vector graphics Asteroids clone, and it's doing good right now. (Except for 2 problems.... 1 is minor... I'll fix it later.. It needs optimization.. BIG TIME.. but it runs good on my P120 so it can wait) The other problem, the main one.. (Ok, it doesn't SEEM so main, but trust me), is with the maximum ship speed code: I have the speed, max_speed set for 5. xv and yv are the X and Y velocities. Right now, if XV is more than 5, it is set to 5 (or, whatever max_speed is), the same for YV... the problem? If XV=max_speed and YV=max_speed then you are (Or -XV = max_speed, etc) going abs(YV)+abs(YV)=10!!! This also brings some other problems into play. I would like some code or algorithm with the needed math to keep this from hapenning. I want abs(YV)+abs(XV) <= max_speed. I know this can be done, it happens all the time in games like Micro Machines. (And I saw it done in Drift, a cool Windows Asteroids clone..) I tried some, but I the ship jerks around when I let off thrust and turn around and apply thrust... which is worse than the current problem. Also, with this problem, if you are say, facing a 41 degree angle, then XV will max out soon, but YV will keep increasing until it is maxed out. Weird looking effect. And when both velocities are maxed out, let's say at +5 and +5, then so long as you are facing a direction that will increase XV or YV past max_speed, then you won't change velocity until you move far enough to have the velocity slow you down. (Hope I put that good enough so you can understand.... I can't seem to describe the problem too well) Anyway, I hope somebody can answer the question... (Er, fix the problem... there really wasn't a question.. :) I'll send a shrouded version if you want to try the game.... (And if I trust ya, the source.. heehee ;) Thanks in advance (And THANKS for at least reading this.. and trying to understand it.. :)
2. Re: Math question for game...
- Posted by Michael Packard <lgp at EXO.COM> Sep 12, 1997
- 803 views
as for the speed, one way to do it is to keep track of the absolute speed, say velocity and then just set your xv = sin(angle)*velocity and yv=cos(angle)*velocity. In OidZone, I have a precomputed angletable with these differential offsets in it. Michael Packard Lord Generic Productions
3. Re: Math question for game...
- Posted by Robert B Pilkington <bpilkington at JUNO.COM> Sep 15, 1997
- 885 views
>as for the speed, one way to do it is to keep track of the absolute >speed, say velocity and then just set your xv = sin(angle)*velocity and >yv=cos(angle)*velocity. In OidZone, I have a precomputed angletable with >these differential offsets in it. Wouldn't that have a different effect? I tried that and variations on it but all I got was a 'jumping' effect. If I'm going one direction, at full speed, and turn, then apply thrust, I immediately go full speed in the new direction. I want more of an original Asteroids feel. (Or, Asteroids with a maximum speed) In the same mail send, I'm using an Internet by E-Mail server to check out the site you posted.... Perhaps I can find out what I need. (Did I impliment it correctly? xv = sin(angle)*(abs(xv)+abs(yv)) [Actually, for some reason I need to use cos for xv and sin for yv.... must be due to the book I read's way of doing it.])
4. Re: Math question for game...
- Posted by Pete Eberlein <xseal at HARBORSIDE.COM> Sep 18, 1997
- 841 views
Robert B Pilkington wrote: > > >as for the speed, one way to do it is to keep track of the absolute > >speed, say velocity and then just set your xv = sin(angle)*velocity and > >yv=cos(angle)*velocity. In OidZone, I have a precomputed angletable > with > >these differential offsets in it. > > Wouldn't that have a different effect? I tried that and variations on it > but all I got was a 'jumping' effect. If I'm going one direction, at full > speed, and turn, then apply thrust, I immediately go full speed in the > new direction. I want more of an original Asteroids feel. (Or, Asteroids > with a maximum speed) > In the same mail send, I'm using an Internet by E-Mail server to check > out the site you posted.... Perhaps I can find out what I need. > > (Did I impliment it correctly? xv = sin(angle)*(abs(xv)+abs(yv)) > [Actually, for some reason I need to use cos for xv and sin for yv.... > must be due to the book I read's way of doing it.]) In Physics, there are three things you keep track of for moving objects: position, velocity and acceleration. If velocity is zero the position is not changing, and the object is not moving. If the velocity is non-zero and acceleration is zero, the object moves at constant speed in the same direction. If the acceleration is nonzero, then the velocity is changing, and the object is speeding up or slowing down. In one of Michael Packard's games, the acceleration of your ship also has a gravity factor so you are drawn to the big star in the middle. The following code shows how I would implement a two-dimensional moving object limited by a maximum velocity. ---- code begins ---- constant max_velocity_squared = 25 * 25 sequence position, velocity, new_velocity postion = repeat(0,2) --x,y paired sequence veloctity = repeat(0,2) -- same here while game_loop do if thrust_button_pressed then new_velocity = velocity + some_constant * {cos(angle),sin(angle)} -- ^--- this is the acceleration --------^ if new_velocity[1]*new_velocity[1] + new_velocity[2]*new_velocity[2] < max_velocity_squared then velocity = new_velocity end if end if position = position + velocity -- draw ship or whatever end while -- game_loop ---- code ends ---- -- _____ _____ _____ ________ /\ \ /\ \ /\ \ / \ \ / \____\ / \____\ / \____\ / _ \____\ / / ___/_ / /____/ / / ___/_ / / \ |____|/ / /\____\ / \ \ / / /\____\ \ \_/ / / \ \/ / ___/_\ \ \ \ \/ / ___/_ \ /____/ \ / /\ \\/\ \ \ \ / /\ \ \ \ \ \ \/ \____\ \ \ \ \ \/ \____\ \ \ \ \ / / \ \____\ \ / / \ \____\ \ / / \ / / \ / / \ / / \ / / \/____/ \ / / \/____/ \/____/ \/____/
5. Re: Math question for game...
- Posted by Robert B Pilkington <bpilkington at JUNO.COM> Sep 22, 1997
- 814 views
On Thu, 18 Sep 1997 13:07:51 -0700 Pete Eberlein <xseal at harborside.com> writes: >In Physics, there are three things you keep track of for moving >objects: >position, velocity and acceleration. If velocity is zero the position >is not changing, and the object is not moving. If the velocity is >non-zero and acceleration is zero, the object moves at constant speed >in the same direction. If the acceleration is nonzero, then the velocity >is changing, and the object is speeding up or slowing down. In one of >Michael Packard's games, the acceleration of your ship also has a >gravity factor so you are drawn to the big star in the middle. The >following code shows how I would implement a two-dimensional moving >object limited by a maximum velocity. Yeah, I knew that... Just forgot to say that part..... :) > >---- code begins ---- >constant max_velocity_squared = 25 * 25 >sequence position, velocity, new_velocity >postion = repeat(0,2) --x,y paired sequence >veloctity = repeat(0,2) -- same here >while game_loop do >if thrust_button_pressed then > new_velocity = velocity + some_constant * {cos(angle),sin(angle)} >-- ^--- this is the acceleration --------^ > if new_velocity[1]*new_velocity[1] > + new_velocity[2]*new_velocity[2] > < max_velocity_squared then > velocity = new_velocity > end if >end if >position = position + velocity >-- draw ship or whatever >end while -- game_loop >---- code ends ---- This has the effect of keeping the abs(XV) + abs(YV) <= TOP_SPEED, but one bad side effect: If I head, say, left and get to full speed, then turn facing down, holding down thrust, I don't chance direction (velocity-wise)!!! If I turn enough, the direction will change, but it looks funny and is too noticable...... :) In my code: player[XV]=player[XV]+(player_accel * cos_lookup[player[ANGLE]] player[YV]=player[YV]+(player_accel * sin_lookup[player[ANGLE]] if player[XV] > player_top_speed then player[XV] = player_top_speed elsif player[XV] < -(player_top_speed) then player[XV] = -player_top_speed end if if player[YV] > player_top_speed then player[YV] = player_top_speed elsif player[YV] < -(player_top_speed) then player[YV] = -player_top_speed end if If I head left, then turn down, I will go too fast (only noticable if you are looking for it, the casual player will not notice this), then heading down, my velocities will be: player[XV] = -player_top_speed player[YV] = player_top_speed Add the absolute values together and I'm going twice the speed I should be! The second glitch from this is: If my angle is 1, XV will max out quick, but slowly, YV will keep on increasing until I'm going at a 45 degree angle. And, as long as the thrust for my angle will increase XV and YV in the same direction on the number line (1 to 89 degrees), my direction won't change. (I can turn left and right while holding thrust, and so long as player[ANGLE] stays within 1 to 89 (or 90 to 179 or whatever), my ship won't change direction, and that looks funny. Is what I need, is at least an algorithm, that will slowly change my speed to the direction thrust is. If I head max speed in one direction, then hold thrust and turn, I will head to max speed in that direction, no more (as in my code), no less.... :) Oh, and a technical question: Are the lookup tables necessary? I mean, are sin and cos already optimized with lookup tables so I don't need to compute them, or are they standard and the lookup tables actually help performance? Thanks in advance -- again! :)