Avatar billede fredand Forsker
30. maj 2003 - 14:34 Der er 8 kommentarer og
1 løsning

Kordinate Calculation with trigonometri

Hello!

I really suck at trigonometri.

So I hope that I could get some help to calculate some cordinates for a little Java-Game that I'm trying to write.

I got a little tank that moves around, but now I need some collision detection so my plan is to keep track of the corner cordinates. But I do not know how to calculate it, cause the tank is twisting.

I got 2 image that illustrates the scenario, the pixle at the corner inside the red circle is the one that I would like to keep track of:
http://medlem.spray.se/fredand44/1.gif
http://medlem.spray.se/fredand44/2.gif

The data that is known is:

gameWindowWidth
gameWindowHeight
tankWidth
tankHeight
gameWindowCenter
tankTwistingAngle

So if any one could give me a formula to calculate the shifting values for that cordinate, that is dynamic I would be most grateful.

Best regards
Fredrik
Avatar billede fangel Nybegynder
30. maj 2003 - 14:46 #1
and do you have a cordinat for the center og the tank... that would indeen help a lot...

Morten
Avatar billede fredand Forsker
30. maj 2003 - 14:49 #2
Hello Morten!
Yes the center is also known:
centerX = gameWindowWidth/2
centerY = gameWindowHeight/2
That is also the center of the tank.
Best regards
Fredrik
Avatar billede bearhugx Nybegynder
30. maj 2003 - 17:01 #3
The following is untested but taken from a tutorial on 2d transformation, translocation and rotation...

If one defines four corners - calling them
  NorthWest, NorthEast, SouthWest and SouthEast

then the coordinates at 0 degrees twisting would be
NW ( centerX - 0.5*tankWidth, centerY - 0.5*tankHeight );
NE ( centerX + 0.5*tankWidth, centerY - 0.5*tankHeight );
SW ( centerX - 0.5*tankWidth, centerY + 0.5*tankHeight );
SE ( centerX + 0.5*tankWidth, centerY + 0.5*tankHeight );

Then - if centerX and centerY is set as the point of origin for the rotation, the coordinates for the points vill be
NW ( -0.5*tankWidth, -0.5*tankHeight );
NE ( 0.5*tankWidth, -0.5*tankHeight );
SW ( -0.5*tankWidth, 0.5*tankHeight );
SE ( 0.5*tankWidth, 0.5*tankHeight );

one can then use the two rotation formulas
  x' = cos(v) x - sin(v) y
  y' = sin(v) x + cos(v) y

lets say, that the coordinate for SE is (5, 8) and the angle is 45 degrees
then the SE point after rotation will be
  ( cos(45)*5 - sin(45)*8 , sin(45)*5 + cos(45)*8 )
or
  ( -4 , 0 ) approx

these coordinates are then translocated to the center again be adding centerX and centerY accordingly....

Hope this helps...

/Søren
Avatar billede bearhugx Nybegynder
31. maj 2003 - 18:40 #4
Hmmm seems like i've made a bit of a bug there.... (my calculator was set to radians instead of degrees...)

the correct result for point(5,8) after a rotation af 45 degrees should be
  x' = 5 cos 45 - 8 sin 45 = -2.1213
  y' = 5 sin 45 + 8 cos 45 =  9.1923

so the resulting point would be (-2, 9)

/Søren

PS: a tip when translating to a computerscreen - after you are done rotating your points then use negative y values ( multiply your y coordinates with -1) ... In a graph - the y axis increments upwards, while the y axis on a computerscreen increments downwards ... therefore the "MULTIPLY -1 on y coordinates" tip :-)
Avatar billede bearhugx Nybegynder
31. maj 2003 - 18:43 #5
remember, that rotation is counter-clockwise - if you desire clockwise rotation, you can use -1*angle as angle
Avatar billede bearhugx Nybegynder
03. juni 2003 - 02:09 #6
Answer .... Could you use the formulas ???
Avatar billede fredand Forsker
03. juni 2003 - 08:33 #7
Hello Soren!
I should try to implement your formulas as soon as possible, I bet it will work.

I'll be back!
Thanks
Fredrik
Avatar billede fredand Forsker
03. september 2003 - 16:26 #8
Thanks
Avatar billede bearhugx Nybegynder
03. september 2003 - 16:33 #9
thanks for the points - were you able to use the formulas ?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester