Computer Science Canada Please Help With My Game!!! Asap |
Author: | klxxguitarxx [ Sat Dec 15, 2012 10:33 am ] | ||
Post subject: | Please Help With My Game!!! Asap | ||
Please excuse my bad spelling, never been good at it ![]() Can somebody help me out? i think i am going to have to use triginomatry. but im not to sure how to do it. if you could tell me how to do it and write a example in turing that would be great. i think that cos sin and tan a important, but idk ![]() im only working on the physics right now, so the graphics are not to great. the black box is the player. the mouse is target. What is it you are trying to achieve? make a bullet go from the player, to the mouse. but have the line keep going till it reaches the edge What is the problem you are having? Cant figure it out Describe what you have tried to solve this problem alot Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Please specify what version of Turing you are using 4.1.2 |
Author: | TerranceN [ Sat Dec 15, 2012 1:35 pm ] | ||||||||||||
Post subject: | Re: Please Help With My Game!!! Asap | ||||||||||||
The first thing you should understand is how to use an angle and a distance to get a position relative to another. In other words, how to make a line of some length that's at some angle. Then I will show you how to get the angle between two points, which when combined will allow you to make any length of lines that point in the same direction as the mouse. Take a look at the picture here. What we want to figure out is, given position A and the angle at A, and the distance between A and B, find position B. Figuring this out would allow us to draw lines of a specific length, at a specific angle, which will become really useful later when we find the angle between a point and the mouse. First of all, the distance between A and B is just c. Next, since the line segments a and b are aligned with our axis (the x-axis and y-axis) we can move b along the x axis and a along the y axis then we get to position B. So if we find out a and b then Bx = Ax + b and By = Ay + a, where Bx and By are the coordinates of B and Ax and Ay are the coordinates of A. To find b, we can rearrange the equation
to get
similarly
We can then apply this like so:
Now we just need to find the angle between the player and the mouse. Once again this can be done with trigonometry. If you go back to the diagram and imagine A as the player and B as the mouse, then we have three different equations to find the angle. Since finding c, the hypotenuse, will require us to find a and b anyway, we might as well use the equation with Tan (which only uses a and b), so you don't need to calculate c. So what are a and b? Simply the difference in the y-axis and x-axis between A and B. Specifically, b = Bx - Ax and a = By - Ay. A fist attempt at writing a function to find the angle from one point to another might look like this:
After using this to find an angle, one can immediately see a problem: when your mouse is left of aX, the angle is the opposite of what we want. Another problem is that when the points are vertical we get a division by 0 error. We can solve the latter problem by checking if dx is 0 then returning either 90 (straight up) or 270 (straight down), depending on the value of dy. The former problem happens because arctand can only return values from -90 to 90. To fix this, we just need to increase the angle by half a rotation when x2 < x1 which is the same as dx < 0. When using degrees, 360 is a full rotation, so 180 is a half rotation. Which gives us this working version of the function:
As for getting the line to draw to the edge of the screen, you can just use a very long length when applying what is explained above. I hope this helped you understand how you can do it, good luck. |