Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sin and cos help
#11
For playing around with sin/cos and simple graphics:

Code:
'  directional movement test
'  using simple screen/draw graphics for demo

option explicit

'$include: "win/gdi32.bi"

#define SC_ESCAPE &h01
#define SC_SPACE  &h39
#define SC_UP     &h48
#define SC_LEFT   &h4B
#define SC_RIGHT  &h4D
#define SC_DOWN   &h50

#define PI  3.141592

sub sprite.moveforward(currentX as long, currentY as long, currentAngle as long, currentSpeed as long)
  dim radians as double
  '  calculate radians from current angle (direction object is facing)
  radians = (currentAngle) * (pi / 180)
  '  calcualte new x/y positions dependant upon the current speed of movement
  currentX = currentX + currentSpeed * COS(radians)
  currentY = currentY + currentSpeed * SIN(radians)
end sub

type sprite
xpos as long
ypos as long
length as long
facing as long
speed as long
end type

dim s1 as sprite
s1.xpos = 200
s1.ypos = 200
s1.length = 20
s1.facing = 0
s1.speed = 0

dim moveX as double
dim moveY as double
dim done as long

screen 12
done = FALSE
do
  '  DRAW sprite with color 0 to "erase"
  DRAW "S4"
  DRAW "BM" + str$(s1.xpos) + "," + str$(s1.ypos)
  DRAW "C0"
  DRAW "TA" + str$(-s1.facing) + "BR5L10U5D10"
  '  process user commands to move sprite
  if multikey(SC_LEFT) then
    s1.facing = s1.facing - 1
  end if
  if multikey(SC_RIGHT) then
    s1.facing = s1.facing + 1
  end if
  if multikey(SC_UP) then
    s1.speed = 2
  end if
  if multikey(SC_DOWN) then
    s1.speed = -2
  end if
  '  adjust for sprite movement
  sprite.moveforward(s1.xpos, s1.ypos, s1.facing, s1.speed)
  '  keep image on screen
  if s1.xpos < 1 then s1.xpos = 639
  if s1.xpos > 639 then s1.xpos = 1
  if s1.ypos < 1 then s1.ypos = 479
  if s1.ypos > 470 then s1.ypos = 1
  '  draw image on screen
  DRAW "S4"
  DRAW "BM" + str$(s1.xpos) + "," + str$(s1.ypos)
  DRAW "C15"
  DRAW "TA" + str$(-s1.facing) + "BR5L10U5D10"
  '  check for user quit
  if multikey(SC_ESCAPE) then
    done = TRUE
  end if
  sleep 1
  s1.speed = 0
loop until done

Very simple ship movement using arrows left/right for rotation and up for forward thrust in whichever direction the ship is facing.
ature has its way of warning a person away from danger: The distinct black and white coloration on a skunk, the chilling buzz of a rattlesanke, a redneck handing you his beer and saying "Watch this!"
Reply
#12
In the first code posted, in the

Code:
FOR Angle = 2/Pi STEP Pi/12

What was that all about?!
f a fly walked, would it be called a walk?
Why dosn't someone make a word that rymes with purple or orange?
WHY AM I SO ANNOYING? Becuase I wanna!
Why am I typeing this? Cuz im bored!
Reply
#13
Do you mean

Code:
For angle = 0 to 2*pi step Pi/12

That is just walking around the circle (2*Pi radians in a circle) every 15 degrees. He asked how to move a dot around in a circle and mathematically that is one way.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)