Re: Math question for game...

new topic     » goto parent     » topic index » view thread      » older message » newer message

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 ----
--
                   _____         _____         _____
    ________      /\    \       /\    \       /\    \
   /   \    \    /  \____\     /  \____\     /  \____\
  /  _  \____\  /   / ___/_   /   /____/    /   / ___/_
 /  / \  |____|/   / /\____\ /    \    \   /   / /\____\
 \  \_/ /    / \   \/ / ___/_\     \    \  \   \/ / ___/_
  \    /____/   \    / /\    \\/\   \    \  \    / /\    \
   \   \    \    \   \/  \____\  \   \    \  \   \/  \____\
    \   \    \    \      /    /   \   \____\  \      /    /
     \   \____\    \    /    /     \  /    /   \    /    /
      \  /    /     \  /    /       \/____/     \  /    /
       \/____/       \/____/                     \/____/

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu