• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CryoGenesis

Weapon Targeting using Trigonometry?

15 posts in this topic

Hello, I need some help with weapon targeting.
Say, for instance, I have a turret.
This turret knows it's position(x,y)
where it is looking (in degrees)
and where the enemy is (x,y)
Does anyone know the formula to find out the new degrees of the turret based on the enemy's position against the turrent's current degree and position?
Anyone who helps me will be rewarded a +1 rep :)
0

Share this post


Link to post
Share on other sites
Oh and I have another question.

Say I have a blank circle. There is a line going from the middle of the circle going outwards based on an angle.
So the circle's radius is something like 20*20 pixels
the circles x and y position is 0,0
so the first point of the line is 10,10 (the middle of the circle)
How do I work out the second point of the line based on the circles angle (lets just say 90 degrees for example).

I think the equation involves PI but I'm not too good with this sort of math
0

Share this post


Link to post
Share on other sites
[quote name='CryoGenesis' timestamp='1334163223' post='4930300']
Oh and I have another question.

Say I have a blank circle. There is a line going from the middle of the circle going outwards based on an angle.
So the circle's radius is something like 20*20 pixels
the circles x and y position is 0,0
so the first point of the line is 10,10 (the middle of the circle)
How do I work out the second point of the line based on the circles angle (lets just say 90 degrees for example).

I think the equation involves PI but I'm not too good with this sort of math
[/quote]
You're going to have to reformulate, I don't really see what you mean. Do you mean intersecting a line with a circle and finding both intersection points? Does the line always pass through the circle's center?
0

Share this post


Link to post
Share on other sites
Ok I'll try to rephrase.

I want to work out a second point of a line based on a angle.
So I have the first point of a line x and y.
and I want to convert an angle in Degrees to an x position and y position.
other variables you have is the max length of the line (the radius of a circle) and the first point of the line (the centre of a circle : x = (Cx + (Cw/2) ) y = (Cy + (Cw/2) )
I want to find this formula out so I can see where the turret is looking using a line.
0

Share this post


Link to post
Share on other sites
Well if you have an initial point (x, y) and an angle theta, then the line's direction vector is just going to be (cos(theta), sin(theta)). You just add that vector to the initial point to produce a second point for the line (x + cos(theta), y + sin(theta)).

If you need the segment between (x, y) and (x + cos(theta), y + sin(theta)) to be bigger, you can scale it by an arbitrary factor c by multiplying the direction vector by c, therefore obtaining (x + c * cos(theta), y + c * sin(theta)) for the second point.

Btw the centre of a circle is generally defined as just (x, y), with bounds (x +- radius, y +- radius). Makes the math easier.
1

Share this post


Link to post
Share on other sites
Just one note about using angles: If you think about vectors instead, your code will have fewer special cases, it will be easier to read and easier to get right.

As an example, after you use atan2 to obtain an angle, whenever you actually want to use the angle for anything, you'll probably need to compute its sine and cosine. If you store a unit vector instead, its coordinates are precisely (cos(angle), sin(angle)), and you save yourself the trouble of going back and forth between vector and angle representation.

If you need help "deanglifying" some piece of code, I'll be happy to assist.
0

Share this post


Link to post
Share on other sites
Hey, do you know of any web tutorials on this because I cant seem to get this working -_-
The arctan doesn't return an angle in degrees it returns a float between -1 and +1.
Can you explain the vector version? The vector version sounds a lot simpler.
0

Share this post


Link to post
Share on other sites
The arctangent returns the angle in radians. If you are going to use angles, using radians is the natural choice for several reasons. But if you use vectors, none of that matters.

The description of your problem is still a bit confused. The radius is not 20*20 pixels: It's just 20 pixels. It's unclear what you mean by the "position" of the circle. One usually specifies where the circle is by its center. And I don't know what you mean by the first point and the second point. It reminds me of a scene in Analyze This ("Are you talking about the first thing or the second thing?"). :)

Perhaps a picture would make it all clear...
0

Share this post


Link to post
Share on other sites
[img]http://img841.imageshack.us/img841/1246/examplei.png[/img]
The P1 represents the first point of a line (a line has two points x1,y1 and x2, y2)
The line's position represents the circle's angle which should point to the target.
This is so turrets can turn to point at an enemy and shoot at it.

I want to work out:
the x and y position of the lines second point using the circle's angle.
work out the circle's angle using the target's x and y position.

I want to work out the x and y position of the second point so the turret can set a trajectory for bullets.
I want to work out the angle of the circle so I can rotate the turrets image based on its angle.
0

Share this post


Link to post
Share on other sites
[list]
[*]Compute the vector V = T - P1
[*]Normalize V . I.e., multiply each component by 1/length(V), where the length of a vector (x,y) is sqrt(x*x+y*y).
[/list]
You can use this vector V to spew bullets in that direction. The velocity of the bullet would be V*bullet_speed. [EDIT: And the point you were looking for is P1+radius*V (sorry, I forgot).]

As for the rotation of the image, if you are using an API that takes an angle, you'll have to use atan2(V.y, V.x) to get it. The default pose of your turret should point to the right so things work out well. If the API expects the angle in degrees, you need to multiply by 180/PI before you call it. The sad thing is that the first thing your API will do with that value is multiply it by PI/180 to convert it back to radians, and then probably compute its cosine and sine, which are V.x and V.y.
1

Share this post


Link to post
Share on other sites
I was having the same issue a few weeks ago and it was solved here

[url="http://www.gamedev.net/topic/622356-targeting-for-shooting/page__p__4924937"]http://www.gamedev.net/topic/622356-targeting-for-shooting/page__p__4924937[/url]
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0