Sine/Cosine

BlitzPlus Forums/BlitzPlus Beginners Area/Sine/Cosine

Buggy(Posted 2006) [#1]
Apparently math class last year wasn't all useless... however, if I had known that at the time, maybe I would have paid attention.

I'm trying my hand at a 2D space shooter (yes, you can groan) - as if this world didn't have enough of them.

My problem is the max speed of the ship. It messes everything up. Sometimes it doesn't allow the ship to move in a specific direction, and it is definitely messed up. For example, when facing right, the ship's y movement should be zero, no?

I've been stuck for a while, and any help would be much appreciated. Thanks!

Here's the full code:

Graphics 800, 600

TFormFilter 0;Gets rid of that rascally man, Mr. T-form

SeedRnd MilliSecs()

Const leftkey = 203, rightkey = 205, upkey = 200, downkey = 208
Const spacebar = 57


Global globaltimer

Type ship;sets up the type for the player

Field x, y
Field dx, dy
Field rotations
Field image
Field frame
Field direction
Field totalframehunks;total rotations of ship in image
Field framehunk;current one (see above)
Field shootingframehunk;the framehunk that it needs to have to shoot
Field turnspeed;different ships turn at different rates
Field turntimer;how much time has elapsed since the last turn...
Field shootspeed;see turnspeed
Field shoottimer;see turntimer
Field chargeupshooter;if it is a charge-up shooter or not. 1 is yes, 0 is no.
Field chargeup;if it's a charge-up shooter, then this becomes 1 if it is in the chargeup sequence.

End Type

Global ship.ship = New ship;actually creates the ship that the player uses

ship\x = 0
ship\y = 0
ship\dx = 0
ship\dy = 0
ship\rotations = 32
ship\totalframehunks = 4
ship\shootingframehunk = 4
ship\image = LoadAnimImage("TotalShip.bmp", 175, 175, 0, ship\rotations * ship\totalframehunks)

MaskImage ship\image, 255, 0, 0

ship\direction = 0
ship\framehunk = 1
ship\frame = 0
ship\turnspeed = 50
ship\turntimer = MilliSecs()
ship\shootspeed = 1000
ship\shoottimer = MilliSecs()
ship\chargeupshooter = 1
ship\chargeup = 0





Dim xtopspeedtable#(ship\rotations)
Dim ytopspeedtable#(ship\rotations)

;gets the sine and cosine tables

;Run through the full rotation cycle, and use Sin and Cos at increments.
;This helps to speed up the game because the compy doesn't have
; to compute the Sine or Cosine in Real-Time. Poor ol' compy.

multiplyer = 360/ship\rotations

Dim xSinTable#(ship\rotations)
Dim yCosTable#(ship\rotations)

For angle = 0 To ship\rotations - 1;through each and every one.

xSinTable#(angle) = Sin(angle*multiplyer)
yCosTable#(angle) = -Cos(angle*multiplyer);negates the Cosine so the ship doesn't spin oddly.

Next




precomputetopspeedtables(2);THIS IS THE PROBLEMATIC CODE THING!! HELP!!



SetBuffer BackBuffer();Makes it work

Global startingtime = MilliSecs()
Global timepassed

While Not KeyDown(1);Let's begin!

globaltimer = MilliSecs()

If globaltimer > startingtime

timepassed = globaltimer - startingtime


Cls


testkeys()

moveship()

draw()


Flip

startingtime = globaltimer

Delay 15

EndIf


Wend



Function draw();draws all images



DrawImage ship\image, ship\x, ship\y, ship\frame

Text 400, 0, "Ship\dx: " + ship\dx
Text 400, 10, "Ship\dy: " + ship\dy
Text 400, 400, "X-Top Speed: " + xtopspeedtable#((ship\frame / ship\framehunk))
Text 400, 410, "Ship X-movement: " + ship\x + (ship\dy * timepassed)
Text 400, 420, "Time passed: " + timepassed
Text 400, 430, "Y-Top Speed: " + ytopspeedtable#((ship\frame / ship\framehunk))
End Function

Function moveship()

If ship\dx > xtopspeedtable#((ship\frame / ship\framehunk))

ship\dx = xtopspeedtable#((ship\frame / ship\framehunk))

EndIf

If ship\dy > ytopspeedtable#((ship\frame / ship\framehunk))

ship\dy = ytopspeedtable#((ship\frame / ship\framehunk))

EndIf



ship\x = ship\x + (ship\dx * timepassed)
ship\y = ship\y + (ship\dy * timepassed);move in realtime

End Function

Function testkeys();tests all key presses

;SIDE-TO-SIDE MOVEMENT

If globaltimer > ship\turntimer + ship\turnspeed;if it's been long enough for
;the ship to turn again
If KeyDown(rightkey);rotates ship clockwise

ship\frame = ship\frame + 1

If ship\frame > (ship\rotations * ship\framehunk) - 1;nifty code for getting
;the frames right

ship\frame = (ship\rotations * ship\framehunk) - ship\rotations

EndIf

EndIf

If KeyDown(leftkey);rotates ship counter-clockwise

ship\frame = ship\frame - 1

If ship\frame < (ship\rotations * ship\framehunk) - ship\rotations;gets the frames right

ship\frame = (ship\rotations * ship\framehunk) - 1

EndIf

EndIf

ship\turntimer = globaltimer;resets the turning timer, so the ship has to wait
;the "ship\turnspeed" in milliseconds

EndIf

;FORWARD MOVEMENT
If KeyDown(upkey)

ship\dx = ship\dx + (xSinTable#((ship\frame / ship\framehunk)))
ship\dy = ship\dy + (yCosTable#((ship\frame / ship\framehunk)))

EndIf
;SHOOTING MOVEMENT

If globaltimer > ship\shoottimer + ship\shootspeed;if it's been long enough for
;the ship to shoot again

If ship\chargeupshooter = 1;if the ship charges up to shoot

If KeyDown(spacebar);enters shooting chargeup or shifts to next phase of shooting

ship\frame = ship\frame + ship\rotations;moves forward one "framehunk"
ship\framehunk = ship\framehunk + 1

If ship\framehunk > ship\shootingframehunk;if the frame is bigger than
;the total frames in the image

ship\frame = ship\frame - ship\rotations;reset the frame!
ship\framehunk = ship\shootingframehunk


EndIf

EndIf

EndIf

ship\shoottimer = globaltimer

EndIf

If Not KeyDown(spacebar);if they're not pressing spacebar...

If ship\framehunk = ship\shootingframehunk;if they let go of spacebar when gun is charged...

shoot()

EndIf

ship\frame = ship\frame - (ship\rotations * (ship\framehunk - 1));resets the frame
ship\framehunk = 1

EndIf

End Function

Function shoot()

Text 400, 300, "S H O O T !", True, False

End Function

Function precomputetopspeedtables(speedtouse#)
Local speedcontrol#
Local xmaxrange = ship\rotations
Local xangle = xmaxrange
Local yangle = 0

; setup our speed modifier
speedmodifier# = (speedtouse# / ship\rotations)
; save our Top Speed
speedcontrol# = speedtouse#

For angle = 0 To ship\rotations

; get that absolute value of our current speed control
; minus the speed modifier
speedcontrol# = Abs(speedcontrol# - speedmodifier#)

; if it's below a certain level, just set it to zero
If speedcontrol# < .0001 Then
speedcontrol# = .000000
EndIf


; assign the speed control per x,y angle
xtopspeedtable#(xangle) = speedcontrol#
ytopspeedtable#(yangle) = speedcontrol#

; make sure that arrays are being loaded
; in opposite directions
xangle = xangle - 1
yangle = yangle + 1


Next

End Function

End;Closinnng Tiiiime....




EDIT: I really don't understand some of the max speed stuff... I kind of took it from krylar's book without fully understanding it, and tweaked it (a bad idea).

Also, can someone tell me how to make that blue code box on the forums?


CS_TBL(Posted 2006) [#2]
<code>
</code>


but preferred for large chunks o' code:
<codebox>
</codebox>

replace <> with []


mudcat(Posted 2006) [#3]
one thing I see after a quick look over is when you use cos and sin,you need to make your ships dx and dy a float.In fact you need to make everything that has to do with your movement floats.I've ran into a similar problems a few weeks back.
Reason is,if your dx is an int,and you do float math ,it will round it to the nearest whole number.

Hope this helps,
mudcat


mudcat(Posted 2006) [#4]
Here's something to paste and run.
Graphics 800,600
 SetBuffer BackBuffer()
x#=GraphicsWidth()/2
y#=GraphicsHeight()/2

While Not KeyDown(1)
	Cls
	Line x-100,y,x+100,y
	Line x,y-100,x,y+100
	Line x,y,MouseX(),MouseY()
	a#=ATan2(MouseY()-y,MouseX()-x)
	Text 100,100,"angle= "+a
	Text x-100,y,"-100",1
	Text x+100,y,"+100",1
	Text x,y-100-FontHeight(),"-100",1
	Text x,y+100,"+100",1
	Flip
Wend 



Buggy(Posted 2006) [#5]
mudcat-

Thanks, but it doesn't help. I still can't move the ship directly to the right.

By the way, do all <b>html</b> tags work in here?

EDIT: I guess not


mudcat(Posted 2006) [#6]
By the way, do all <b>html</b> tags work in here?


html I gues they do

you have to use [] not <>


Buggy(Posted 2006) [#7]
Ahh...

Thanks, but I'd still like to get back to my main problem...

[marquee]Sweet![/marquee]


Buggy(Posted 2006) [#8]
You guys, I'm desparate! Help!!

Gaaaaaaaaaaaaaaaaah!


Buggy(Posted 2006) [#9]
I took out the top speed completely, and something is wrong with my angles. At 180 degrees, the dx value should be zero, yet it isn't.

I think I can fix it though.


Buggy(Posted 2006) [#10]
It's fixed! Hallelujah!


Grey Alien(Posted 2006) [#11]
btw I did some test of putting sin in an array to read instead of calling Sin realtime and it really was only fractionally faster. In the old days it would have been a lot faster.


Buggy(Posted 2006) [#12]
Wow. Things have gotten fast.