need some math help please

BlitzMax Forums/BlitzMax Programming/need some math help please

InvisibleKid(Posted 2008) [#1]
for simplicity a dumbed down example of my problem is as follows.

you have 2 images 1 is a square (representing a room) and the other is a circle (representing an object in the room).

i want to rotate the square, and have the circle change its x,y values to keep its position in the square.

i hope that makes sense, because i feel i'm not expressing what i mean properly.

here is a picture to hopefully better describe what i mean.



i've tried:
add this
subtract that
multiply this
divide that
sin this
cos that

and i just can't get it to work.

any help would be greatly appreciated.


ImaginaryHuman(Posted 2008) [#2]
You need to decide around what `center` the rotation is going to take place, e.g. the center of the square.

Then you need to use pythagoras or similar to calculate either a vector or an angle-radius pair to measure the distance from the center to the center of the circle.

Then you need to decide how much you are going to rotate the square and apply the rotation to its corners (perhaps again based on an angle and radius from the square's center).

Then you need to add the same angle offset to the angle towards the circle.

Then you can use centerx+(radius*cos(angle)) and centery+(radius*sin(angle)) to reposition the circle.

OR, if you don't care to GET the position of the circle initially, and instead you just want to start out with the square and then position the circle, then you simple can skip the first step or two and just refer to your circle as an angle and radius from the square's center and then the above formula's will position it.


Jesse(Posted 2008) [#3]
[edited]
----if current Angle of Room = 0 degrees-----
dx = Circlex-RoomCenterx 'fixed this
dy = CircleY-RoomCentery
circleAngle = atan2(dy,dx)
RoomCenterToCircleDistance = sqr(dx^2+dy^2)

----when you change RoomAngle--------
NewCircleAngle = CircleAngle + NewRoomAngle

--------new position of circle-------
CircleX = Cos(NewCircleAngle) * RoomCenterToCircleDistance
CircleY = Sin(NewCircleAngle) * RoomCenterToCircleDistance



InvisibleKid(Posted 2008) [#4]
hi guys thanks for the help unfortunately i still can't get the results i'm looking for. i have tried combinations of what both of you have said and other topics i've found in search. the closest i've gotten so far is using this code:

Local x = 180, y = 130		
Local radius = Sqr((400-x)^2 + (300-y)^2)
Local xoffset = 400+(radius*Cos(rot#))
Local yoffset = 300+(radius*Sin(rot#))
		
SetRotation rot#
		
DrawImage myimage ,xoffset,yoffset
		
SetRotation 0


basicly a better description of what i am trying to do is, the square is an 800,600 image which is supposed to represent a sort of 3d looking room, the circle represents an object (person/thing/whatever). i have the code to rotate the room with in a certain set of boundries to represent a sort of POV (this part works).

i'm trying to retain the objects position in the room no matter what the angle is. the above code does retain its position just not in the right spot, even when the rot# = 0 its position isn't x,y its shifted right and down more or less off centre to the right and down, but like i said it does retain its proper position with the rotation just around the wrong x,y values.

every thing else i have tried including both your codes word for word just with my own values just won't work for me.

so i don't know i'm at a loss.

again thanks for the help though, and i'll continue to plug a way at it for a bit but in the long run what i'm trying to accomplish might be mildly amusing at best but i don't know if it would add all that much value to that part of the game or not or if it's even worth the frustration. hopefully i'll get it worked out though.


Yan(Posted 2008) [#5]
Use the handles Luke...


Jesse(Posted 2008) [#6]
you have it correct. you just have to make shure the x, y "handle" is at the center of the circle/object.
in case you didn't understand "Use the handles Luke...", If you are using images use ImageHandle.
If you want the object to be at the same angle corresponding with the room rotation, you have to rotate the object the number of degrees you rotated the room too.


MGE(Posted 2008) [#7]
I don't have the code hanging around, but I call this "rotation with offset" so you just get the rotation angle of the parent multiplied by the distance between the 2 and add that to the child x,y via sin/cos.


InvisibleKid(Posted 2008) [#8]

If you are using images use ImageHandle.



i know MGE does/has but i don't know if you others have used/use greyaliens game framework ( which is pretty cool btw, little plug for him ). which is what i'm using and as far as i know it sets an image's handle to its center automaticly.



you have to rotate the object the number of degrees you rotated the room too.



check, already had that working long before i posted



heres a quick example i whiped up demonstrating a few different things i tried with the results.

just different things i've tried from this post and several other posts that i found in search.

i'm close but not right, and it's getting very frustrating because every example i've seen whether this post or other most of them are fairly similar to each other overall, so i just can't see what i'm doing wrong.




heres the images needed.

mepullingmyhairout.png


room.png



Jesse(Posted 2008) [#9]
having trouble following directions? :).
[edited]
I kind of messed up with my original post but here it is:
[/edited]
Graphics 800,600
AutoMidHandle(True)

Global room = LoadImage("room.png")
Global mepullingmyhairout = LoadImage("mepullingmyhairout.png")

Global x = 225,y = 140
Global xoffset,yoffset
Global rot#
Global testnum = 0


While Not KeyDown(key_Escape)
Cls
	Logic()

	SetRotation rot#
	
	DrawImage room,400,300
	
	DrawImage mepullingmyhairout,xoffset,yoffset
	
	SetRotation 0
	
	DrawText "test ver.: "+testnum,300,10
	
	DrawText "use the left and right keys to change the test and (Esc) to quit",10,580
Flip
Wend
End


Function Logic()
	Local mx = MouseX()
	Local my = MouseY()
	
	If mx > 300 And mx < 500
		If rot# < 0
			rot#:+0.1
			If rot# > 0.0 Then rot# = 0.0
		EndIf
		
		If rot# > 0
			rot#:-0.1						
			If rot# < 0.0 Then rot# = 0.0
		EndIf
	EndIf
	
	If mx < 300 And rot# > -25 Then rot#:-0.1
	If mx > 500 And rot# < 25 Then rot#:+0.1
	If rot# < -25.0 Then rot# = -25.0
	If rot# > 25.0 Then rot# = 25.0

	'
	Local  dx# = x-400 'I messed up here on my original post
	Local  dy# = y-300 
	Local  circleAngle# = ATan2(dy,dx) ' angle from origen 400,300 from previous
	Local  RoomCenterToCircleDistance# = Sqr(dx^2+dy^2) 
	
	Local  NewCircleAngle# = rot# +CircleAngle 
	
	xoffset = 400+(Cos(NewCircleAngle)* RoomCenterToCircleDistance)
	yoffset = 300+(Sin(NewCircleAngle)* RoomCenterToCircleDistance)

End Function

Don't ever add angle and distance. They are two completely different things.

Do a little more research on sin and cos. They will help you a lot figuring things out.


InvisibleKid(Posted 2008) [#10]
beautiful! it works a treat now, thankyou very much everyone.


having trouble following directions? :).

Don't ever add angle and distance. They are two completely different things.



in my code above those are just a small amount of tests that i tried ( i was trying combinations of everything i read up on ), doesn't appear to be in my example but i think at one point i had something pretty much like your above code except the dx,dy values where backwards.


Do a little more research on sin and cos. They will help you a lot figuring things out.



thats just it i've read through alot of posts here, but also over the many many years (probably around 20ish +) that i've been toying with programing, i have never been able to grasp the concept/proper use of sin/cos and the likes. theres alot of things over those years that i picked up on/understood very easily and still remember most now, but like i said unfortunately sin/cos and the like have never been one of them.

looking at it right now its very easy to comprehend and understand, but i'll bet you down the road i'm gonna come up with another problem and figure that it has to do with sin/cos, and i'll post another silly question about how to do this/that.

its annoying but sin/cos never stay with me, i even looked through old code that i had that used them and i was like huh wth did i do here. lol

owell thats just me very thick headed.

i blame my grade 12 math teacher. up until grade 12, i use to love doing math and i was fairly good at it even sin/cos to some extent. but from the first day i walked into that class he had a problem with me just because at that time i had long hair. all semester he gave me a hard time and ended up failing me just because he didn't like me. from then on unfortunatetly i subconciously devoloped a hate for math( which really sucks for someone who likes to program).

he's the only teacher that i've ever had any real problem with and i'm not gonna say what i think of him because that would be improper for this forum, but i'm sure you could guess lol.

thanks again


Jesse(Posted 2008) [#11]
d**n teachers, some could be so abnocsious. I had my own problems with a few but nothing to the degree of failing. anyway good luck with your project.


Warpy(Posted 2008) [#12]
Would people be interested in a very clearly written maths tutorial for games? I've written lots of examples in this forum for people over the years, so I wouldn't mind collecting it all together.


InvisibleKid(Posted 2008) [#13]
Warpy i think thats a wonderful idea. i've always been a big fan of all the infamous Warpy code, and have most of your code examples on my hd somewhere. i think most people would get good use out of such a thing be it beginners, people like me who've been around and doing this for years but just stink at the more complicated/game oriented maths, and probably even some of the better programers on the forum.


Tachyon(Posted 2008) [#14]
Would people be interested in a very clearly written maths tutorial for games? I've written lots of examples in this forum for people over the years, so I wouldn't mind collecting it all together.

Y-E-S.

Heck, I'd pay you for a well-written, BlitzMax coded, nicely explained, all-encompassing collection of game-specific mathematics.