Having a problem with SetRotation

BlitzMax Forums/BlitzMax Beginners Area/Having a problem with SetRotation

delbar(Posted 2005) [#1]
Hello Everyone.

I am trying to make my player(bitmap imaged sprite) able to move in a " direction" after rotating however far left or right(with the left and right arrow keys). I currently have player moving on X and Y with arrow keys, if i hit up and right together he will move diagonally which is fairly good but not exactly what I want. here is the current code for player1's input



My problem begins with making a varible float for "direction " then using " direction" in SetRotation using left or right arrows to add or subtract from "direction" this causes my player and all objects displayed to rotate, even my text showing memalloced() and memusage() and my tiled map. Been trying to figure this out for days and have searched far and wide on the web for some examples to give me a clue.


What I eventually want is to have my player rotate on left and right arrows and then move forward or backwards in the current direction of " direction " and then if I combine a left or right arrow and another key to then strafe left or right + or - 90 degrees from the current facing direction. I think I can handle the strafing part IF I could ever figure out how to move him as described, really gets frustrating being a newbie to this.

Any help is greatly appreciated!

P.S. The code example is what I started with , what I tried was too ugly to be seen in public , plus I scraped it and switched it back to the code you see now.


Tom(Posted 2005) [#2]
The value set with SetRotation affects all subsequent drawing commands. Best idea is to reset it to 0 before drawing text e.t.c

Here's an example of how to move your player in the direction you want.




Tom


delbar(Posted 2005) [#3]
Hello

Thanks a ton Tom, I think the SetRotation problem was sofar the most difficult thing I have tried to deal with in BlitzMax. Your code example helps a ton. Wonder if they might someday make SetRotation with 1 more parameter to include what image your wanting to effect. I think that sure would make things alot easier. But being a newbie to this there may be a good reason they have it the way it is. Thanks again so much for helping me solve this small nightmare .


delbar(Posted 2005) [#4]
Hello again.

Hate to keep bothering you , But if Tom is reading this or anyone who understands 2d vectors, could you please give a hand.




If someone or tom could please explain why the mouseleft command won't move the sprite to strafe -90 degrees from the current direction but the mouseright strafe works perfectly. I am refering to Tom's code example above in his post. I have tried the mouseleft with different numbers such as -90 and the sprite won't move no matter what I do it seems.Have tried changing the +/- on the Cos and Sin to no avail. I have never heard of vectors until Tom fixed my problem so any help explaining why my code won't work will help a ton. Thanks in advance.


Oddball(Posted 2005) [#5]
Simple Trig101:
First of all your starting direction is incorrect. Zero degrees points to the right of the screen. Positive rotation is clockwise and negative is counter-clockwise in true trig it is the opposite directions, but on a computer screen the y-axis is inverted causing the directions to reverse. Cos should always return the x component of a vector and Sin should always return the y component. It is better, but not essential, to represent angles ranging from -180 to +180 as the angle functions, such as the very useful Atan2, return values within this range. The code below cleans Tom's code a little and adds strafing. Just cut and paste the bit you need.

Hope this helps.


Oddball(Posted 2005) [#6]
I should also add delbar that the strafe code you have posted does work with one minor edit. I believe the problem you are experiancing is due to a slight coding error. Here's your code with the error corrected.
	If KeyDown(KEY_CONTROL)&KeyDown(KEY_MOUSERIGHT)
		'use Sin & Cos to convert the angle in degrees to x,y translation values (a 2D Vector)
		px:+Sin(direction+90*movespeed)
		py:-Cos(direction+90*movespeed)
		
	ElseIf KeyDown(KEY_CONTROL)&KeyDown(KEY_MOUSELEFT)	'<<<<<--Edit
		'use Sin & Cos to convert the angle in degrees to x,y translation values (a 2D Vector)
		px:+Sin(direction+270*movespeed)
		py:-Cos(direction+270*movespeed)
	End If
But I would suggest you implement the changes I've outlined in the post above. It will help in the long run.


delbar(Posted 2005) [#7]
Hello

Thank you Oddball for helping me with this. I couldn't understand why the left strafe wouldn't work while the right one did. By your second post I see that I didn't have it " nested " into the code properly with the Else If
in front of it so I am guessing it never recognized the code. Also thank you for letting me know what kind of math a vector is, I will have to buy me a book on it or search the web and learn some trig. it seems if I am going to make games( even if it is just a hobby). I can see where it will certainly help out alot to actually know what your doing with the math to make programming easier. Once again thank you Oddball and Tom for helping me with this code. I am learning quite abit from you all and from programming in general.Lots more ways to go though I see!


delbar(Posted 2005) [#8]
Hello again.

Edited: to let you all know I fixed the problem I was having so I deleted my post. Wasn't initilizing a Field for Direction so my sprite wouldn't rotate.