Need Help With Sin, Cos, and Tan
BlitzMax Forums/BlitzMax Beginners Area/Need Help With Sin, Cos, and Tan
| ||
Could anyone help/show where to find help with this? I can easily find what they are, but I've been having trouble finding out what I can use them for and how. |
| ||
www.google.com Search "computer trigonometry tutorial" or similar http://www.gamespp.com/cgi-bin/resource.cgi?mathTutorials_trig1 for example @ http://www.gamespp.com/ The are generaly used in 2d to find out how far away somthing is. Or to add forces together and find the x or Y component |
| ||
You can use a combination of Cos and Sin to turn an angle and a radius into pixel coordinates. e.g. Graphics 640,480,0 For Local Angle:Int=0 to 359 step 3 Plot 320+(100*Cos(Angle)),240+(70*Sin(Angle)) Next will draw an oval of dots, 3 degrees apart. The 100* and 70* scale the values up because normally you get something in the range of 0 to 1, so to turn it into screen coords you multiply it by the pixel width or height and hey presto. |
| ||
see if this example can help you is not well documented, but it might help you get the idea when you run it. |
| ||
Here's a little "analogue clock" example I threw together... Hope it helps. |
| ||
Brucey: you've written "cock" |
| ||
Sin Cos and Tan are useful for lots of things. Using Sin and Cos, you can plot points using polar coordinates. Specify and angle and a distance from the origin, and the equation will spit out a regular X and Y coordinate for you to use. And SohCahToa tells you that if you have a right triangle and you know the length of the side opposite a particular corner, and the length of the triangle's hypotenuse, (the side opposite the triangle's 90 degree angle) you can calculate the sine. Or if you have the adjacent side (the one touching the corner which is not also the hypoteneues) you can calculate the cosine of that angle. And if you know the sine or cosine of an angle, you can convert that to an angle in degrees. Going back to the polar coordinate example, sine and cosine are used when plotting circles. If you imagine a point orbiting the center of your graph, and consider how it moves on just the Y axis, you will find it accelerates as it approaches the middle and slows down near the edges. That's because Y = Radius * Sin(Angle), and as the point goes around the angle changes, and the sine has that acceleration built in. Once you understand how that works, you can use it to do things like make objects in your game move across the screen in a sine wave, or accelerate when moving from one point to another. The polar coordinate equations I was talking about are these: X = Radius * Cos(Angle) Y = Radius * Sin(Angle) Those will give you numbers ranging from -Radius to +Radius on each axis, becayse Cos and Sin vary from -1 to 1 as you vary the angle. Anyway that should give you a jump start on what you can do with Sin and Cos. As for what you can do with Tan... Tan isn't very useful, but ArcTan is. ArcTan is the inverse of Tan. Instead of passing it an angle, you pass it the length of the two sides of the right triangle. And you get the angle back. So you can pass ArcTan the X and Y coordinate of a point, and it will tell you the angle of that point relative to 0,0. |
| ||
Brucey: you've written... Heh... that's right folks... my mind's always in the gutter... :-) |
| ||
Thanks! I'll be able to understand people's code snippets MUCH better now. |
| ||
I feel a bit embarrassed asking this question, hence the bump to this thread rather than starting a new one, but... I feel like I've almost, but not quite, gotten this one. I'm trying to make a very simplified version of Master of Orion or Reach for the Stars - that kind of game - and I want to move spaceships along a straight line between planets, by a fixed amount each turn, until they're in range of crossing the remaining distance in a single turn. So, I'm thinking that in order to move the ships in this way I need to use trig for a couple of things - firstly, Atan to get the angle between two planets located at x1, y1 and x2, y2, and then for the dx, dy of the ship Cos(angle) and Sin(angle). So, I guess my three questions are, 1) is this right? and 2) is this an overcomplicated way of doing something that could be done more simply and 3) does the following code look right? Method Update() Self.angle = ATan( (destY - y) / (destX - x) ) Self.dx = Self.speed * Cos(Self.angle) Self.dy = Self.speed * Sin(Self.angle) Self.x = Self.x + Self.dx Self.y = Self.y + Self.dy End Method |
| ||
To point 1) and 2): You can solve the problem without any use of functions like Sin() a.o. as shown below (simple parametric movement equations). |
| ||
lesslucid, that looks fine. Most of the enemies in Bullet Candy use something similar for moving around. Cheers Charlie |
| ||
Yes, leeslucid, that's fine, and don't worry about using sin and cos because they're very quick these days. You should use ATan2 instead of ATan though, to avoid divide-by-zero errors. |
| ||
Wow, thanks all very much for your help. Especially xMicky - maybe you can write that much code in your sleep but to me that looks like real work! |
| ||
The code was cut out and thrown together from an engine project I'm working at since a longer time now, so no special amount of work to reply to your question ;) I just wanted to show how to solve the given problem without using trigonometric functions, but of cause there are several different ways to fulfill the task. |