| 08-27-2009, 03:25 PM | #1 |
Hi guys, I am working on 2D3S and I would like some help to rotate a line. I have the coordinates of the last point of the line, all I need is to simple move that point to the new position and then recalculate the line. However I am having problems rotating the last point of the line ... as usual, a code is worth a billion words, so here it is: 2D3S core JASS://=========================================================================== //Description: // - A system that allows the user to create many shapes in a very simple and //customizable way. This is simply the core of the system. To draw more shapes //you will have to import libraries. // //Author: // - Flame_Phoenix // //Credits: // - ThredNThrash, for the main idea and math formulas // - BubleTech, for efficiency advices // - Pyro and Anitarf for suggestions on how to improve // - Veev, for fixing grammar // //@version 1.1.3 //=========================================================================== library SimpleShapes requires LinkedList struct Point private real pointx private real pointy private real pointTang static method create takes real newX, real newY returns Point local Point this = Point.allocate() //setting members set .pointx = newX set .pointy = newY return this endmethod //Getters method operator x takes nothing returns real return .pointx endmethod method operator y takes nothing returns real return .pointy endmethod method operator tang takes nothing returns real return .pointTang endmethod //Setters method operator x= takes real newX returns nothing set .pointx = newX endmethod method operator y= takes real newY returns nothing set .pointy = newY endmethod method operator tang= takes real newTang returns nothing set .pointTang = newTang endmethod endstruct struct Shape List points static method create takes nothing returns Shape local Shape this = Shape.allocate() //create our list set .points = List.create() return this endmethod method onDestroy takes nothing returns nothing call .points.destroy() endmethod endstruct endlibrary Line, this is where I need help in the rotate method. JASS://=========================================================================== //Description: // - This library allows you to have an object called Line. It has two optional //modules you can import to have extra features: xefxLineModule and //lightningLineModule. // //Requires: // - SimpleShapes // //Methods you can use: // static method createLine takes real sX, real sY, real eX, real eY, real interval //returns Line // - sX and sY and startX and startY where the Line begins. eX and eY stand for endX //and endY respectively. The interval is the space between points. // //Members you can use: // real ANGLE - imagine that you want to move an object accross a line. This is the angle //the object needs to have and it is in radians. To get the inverse of an angle just do //ANGLE + bj_PI. // //Author: // - Flame_Phoenix // //Credits: // - Chobibo, for reporting major problems // //@version 1.2.1 //=========================================================================== library LineShape requires SimpleShapes struct Line extends Shape private real LINE_ANGLE // the tangent line in rads, useful to move projectiles private integer size private real dist implement optional XefxLineModule implement optional LightningLineModule static method createLine takes real sX, real sY, real eX, real eY, real interval returns Line local Line this = Line.allocate() //the distance of the line local real dx = sX - eX local real dy = sY - eY local real dist = SquareRoot(dx*dx + dy*dy) //temporary variables that will allow us to create many points local integer i = 0 //this is to make code easier to read in the loop local real x local real y //this variables are for efficiency reasons local real xAng local real yAng set .size = R2I(dist / (interval ) ) set .dist = dist //setting our members set .LINE_ANGLE = Atan2(eY - sY, eX - sX) set xAng = Cos(.LINE_ANGLE) set yAng = Sin(.LINE_ANGLE) loop exitwhen(i == .size) //polar projection stuff set x = sX + ((i * dist) / .size) * xAng set y = sY + ((i * dist) / .size) * yAng call Link.createLast(.points, Point.create(x, y)) set i = i + 1 endloop return this endmethod //getter method operator ANGLE takes nothing returns real return .LINE_ANGLE endmethod stub method move takes real sX, real sY, real eX, real eY returns nothing //the distance of the line local real dx = sX - eX local real dy = sY - eY local real dist = SquareRoot(dx*dx + dy*dy) //temporary variables that will allow us to create many points local integer i = 0 //this is to make code easier to read in the loop local real x local real y //this variables are for efficiency reasons local real xAng local real yAng //destroy the old list and create a new one with the new postitions call .points.destroy() set .points = List.create() //setting our members set .LINE_ANGLE = Atan2(eY - sY, eX - sX) set xAng = Cos(.LINE_ANGLE) set yAng = Sin(.LINE_ANGLE) loop exitwhen(i == .size) //polar projection stuff set x = sX + ((i * dist) / .size) * xAng set y = sY + ((i * dist) / .size) * yAng call Link.createLast(.points, Point.create(x, y)) set i = i + 1 endloop endmethod stub method rotate takes real AngleDEG returns nothing local real lastX = Point(.points.last.data).x local real lastY = Point(.points.last.data).y local real rotX = lastX*Cos(AngleDEG*bj_DEGTORAD) + lastY*Cos(AngleDEG*bj_DEGTORAD + bj_PI/2) local real rotY = lastX*Sin(AngleDEG*bj_DEGTORAD) + lastY*Sin(AngleDEG*bj_DEGTORAD + bj_PI/2) call .move(Point(.points.first.data).x, Point(.points.first.data).y, rotX, rotY) endmethod endstruct endlibrary |
| 08-27-2009, 04:39 PM | #2 |
Basic trigonometry. Note: all coordinates are relative to the center of rotation:set newx=x*Cos(a)-y*Sin(a) set newy=x*Sin(a)+y*Cos(a) |
| 08-27-2009, 06:16 PM | #3 |
basic !? I would never get to the formula alone ... and I did try reading stuff on wikipedia ... Thx ! |
| 08-27-2009, 06:58 PM | #4 |
Please take this advice gently: If you don't learn BASIC trigonometry and geometry, your system will not go far. |
| 08-27-2009, 09:12 PM | #5 |
What Anitarf referred to is called rotation of axes. It's obtained by using the sum and difference formulas for sin and cos. This stuff is covered usually with conic sections, and was included in my precalc textbook last year. Anitarf's example rotates about the origin - just add the rotation to your base x and y coordinates. |
| 08-27-2009, 10:34 PM | #6 |
Truth is I don't need much for my shape system. I only need to know how lines rotate, after that everything shall be done quite easily because the system will be quite modular. Imagine a Cross shape with a rotate method: JASS:struct cross Line l1 Line l2 stub method rotate takes real degs returns nothing call l1.rotate(degs) //same for l2 endmethod endstruct Sure there may be some more calculus in the middle, but this the main skeleton idea I have. I don't see how you people apply this stuff so easily. I also learned this a long time ago, however, in my teachings everything is always nice in theory - problem is, that's the only thing we ever learn, so technically when it comes to practice, I am the disaster you all see. Further more I am a survivor on one of the universities with hardest maths on my country (2nd ftw ...), yet I can't do a simple calculus like this. I feel stupid ... |
| 08-27-2009, 11:51 PM | #7 | |
Quote:
|
| 08-28-2009, 06:24 AM | #8 |
Flame_Phoenix, before you copy and paste any script into your map, try to understand how that script works. For instance, it may be easier to see how rotation works in this version: JASS:set newx = x*Cos(a) + y*Cos(a + bj_PI/2) set newy = x*Sin(a) + y*Sin(a + bj_PI/2) |
| 08-28-2009, 06:53 AM | #9 |
lol so there is a bj_PI and i was always using bj_DEGTORAD*180. |
| 08-28-2009, 09:45 AM | #10 |
It is hard for me to keep my persistence and good will in studying a subject when everyone says my I.Q. is lower than the I.Q. of a chair... it's simply frustrating. But thanks, I will do my best now. |
| 08-28-2009, 01:25 PM | #11 |
To boost your self-confidence: i started learning trigonometry in the 4 grade of secondary school and i saw this 'basic' trigonometry in the first year physics at university. Just some advice: -When you need trigonometry, draw a trigonometric circle on a paper. -Know the geometric definition of sin and cos.(see link above) Combine this with your brain and you should be able to derive a lot of things from it. Image is from http://nl.wikipedia.org/wiki/Sinus_en_cosinus English page: http://en.wikipedia.org/wiki/Trigonometric_functions (less good images imo) BTW: how is your spell make tutorial v2 going? |
| 09-12-2009, 12:09 PM | #12 |
It is currently on hold. I just want to finish my 2D3S system, my tutorial, and I will probably quit wc3 after. Thx for asking though. Updated the first post. Please give constructive criticism. I am open to ideas. |
| 09-12-2009, 01:08 PM | #13 | |
http://www.ies.co.jp/math/java/trig/...sixtrigfn.html The single most useful thing I have ever found in understanding trig. May be of use to you. It shows the input angle, and the output of the various functions. It also shows you how the output of the functions relates to the unit circle and input angle, visually. Click'n'drag the red circle to change the angle. Quote:
Now, to create an illusion of still being an ass: Go die in some god-forsaken hole, FP. (Hey, I have a reputation to maintain! I can't just go around being nice to people. That would ruin me!) |
| 09-12-2009, 02:34 PM | #14 | |
Well, there is something I wanted to say for a long time... In case you haven't noticed yet (I believe most of you already did) I have no real skills. What I have is my dedication, determination and good will. When people help me around here, they get frustrated because they see my lack of skill and get angry for me, but thing is I can't do better. When someone helps me, all I can give is my determination, dedication and my good will into learning what they are trying to teach me, if this is not enough, if you want more, then it simply won't happen because I can't give what I don't have. This is not to anyone in specific but to all people who feel frustrated because of me. I wanted to say this for a long time, however I never got the chance. To be honest I still think this is kinda off-topic but I just couldn't hold it any longer. Quote:
Thanks for the links btw. Any suggestions on the code? |
| 09-12-2009, 03:15 PM | #15 | |||
Quote:
Now, don't prove me wrong, otherwise I will have to castrate you. Quote:
Quote:
|
