sin and cos

BlitzPlus Forums/BlitzPlus Programming/sin and cos

can someone give me a bit of coe that will make a car turn please


thanks

If RoadLeft= True
    Dir=(Dir-1) Mod 360
Elseif RoadRight = True
    Dir=(Dir+1) Mod 360
endif


(-1 mod 360) = -1

What you meant is:

If RoadLeft= True
    Dir=(Dir+359) Mod 360
ElseIf RoadRight = True
    Dir=(Dir+1) Mod 360
EndIf


Tho I'm not sure about the status of superjossy's skills, and whether he's one of the many who's driving too fast in his first drivinglessons.. :P

What you meant is:
No I meant mine, for ease of understanding. I admit that yours would be an acceptable alternative, (Thou not better) solution

hm.. I guess the choice is whether one wants the angle to stay within the 0..359 range or within the -359..359 range. My alternative is based on the 0..359 range..

ohwell..

Humm.
For me, the choice is if your normal system of use has Dir:+1 implemented as a single command, or if you have memory restrictions and have lookup tables for the subsequent sin/cos equations.

I wrote this as an example. I'm not sure if it is what you ment. You can use the cursorkeys to drive the block around.
	Graphics 800, 600, 0, 2
	SetBuffer BackBuffer()
	
	;if this is 1, the program will generate 360 image; one for each angle
	Const anglesteps = 5

	;make original car	
	orgcar = CreateImage(128, 128)
	
	Rect 40, 32, 48, 64, 0
	Rect 42, 34, 44, 22
	
	GrabImage orgcar, 0, 0	
	MidHandle orgcar
	
	;make 72 rotated images	(360 / 5)
	Dim car(360)
	
	For i = 0 To (360 / anglesteps) - 1
	
		tempcar = CopyImage(orgcar)
		RotateImage tempcar, (i * anglesteps) + 90
		Cls
		DrawImage tempcar, 48, 48
		car(i) = CreateImage(80, 80)
		GrabImage car(i), 8, 8
		Rect 8, 8, 80, 80, 0
		MidHandle car(i)
		FreeImage tempcar
		Flip
		
	Next
	
	;best is, to save these images onto disk so you only need to generate them once.
	
	Cls
	
	carangle = 0
	x# = 400
	y# = 300
	speed# = 5
	
	;main loop
	Repeat

		;draw car image	
		Cls
		carframe = carangle / anglesteps
		DrawImage car(carframe), x, y
		Flip

		;up/down		
		If KeyDown(200) Then 
			x = x + Cos(carangle) * speed
			y = y + Sin(carangle) * speed
		End If
		If KeyDown(208) Then 
			x = x - Cos(carangle) * speed
			y = y - Sin(carangle) * speed
		End If
		
		;left/right
		If KeyDown(203) Then carangle = carangle - 2
		If KeyDown(205) Then carangle = carangle + 2
		If carangle < 0 Then carangle = 359
		If carangle > 359 Then carangle = 0
	
	;esc=exit
	Until KeyHit(1)
	
End


;left/right
		If KeyDown(203) Then carangle = carangle - 2
		If KeyDown(205) Then carangle = carangle + 2
		If carangle < 0 Then carangle = 359
		If carangle > 359 Then carangle = 0


Are you sure? carangle -2 could be like -2. The correct value should then be 358. +2 and +358, and one single modulo.. :P

Thanks b32 !!

It is very usefull for me too :)))

But I don't understand CS_TBL, what would you change in the b32 code ?

Well, the issue is this:

He changes angles in steps of 2, rite?
So, possible values are 8 6 4 2 0 -2 -4 -6 -8.

Next he clips them so that <0 would becomes 359 and >359 would become 0. And there's the problem.

A value of -2 becomes 359. The distance from -2 to 0 = '2', the distance from 359 to 0 = '1', so there's where angle errors come from.

What I would do:

option 1:

		If carangle < 0 Then carangle = 358
		If carangle > 358 Then carangle = 0


But his option is hardcoded for angle-rotations of 2, not
exactly elegant.


option 2: have a 'speed' variable somewhere, with the value '2'.

If KeyDown(203) Then carangle = (carangle + (360-speed)) Mod 360
If KeyDown(205) Then carangle = (carangle + speed) Mod 360


In this solution you can change the speed whenever you want.

'speed' could be any name tho, such as 'angledelta'..

Thanks a lot ;)

CB, you are right offcourse. :)
Ow .. and, Reno, if you want to rotate masked images, you should use "TFormFilter 0", else the images are smoothened and the transparent background gets blended with the foreground.
And I don't know if you're familiar with the LoadAnimImage command ? With LoadAnimImage, you could use one single (big) image that contains all the prerotated frames. It's much easier than loading 180 separate images.

Thanks, but I knew that already ;)