• 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
Felipe M

Moving a projectile from an angle.

11 posts in this topic

Hi, I'm trying to make something like a game... where I have a sort of a cannon that shoots projectiles, and this cannon is constantly rotating around itself, and around a point in the center of the screen. And it's 2D


Well, I have a bullets vector, that I can get the X an Y position of them. When I create every bullet I create them in the cannon X and Y position, and I also keep the angle that the cannon was in another vector.

The question is... how do I move them in that angle?

I do not want to use gravity or anything like that, just move them in a constant velocity in the direction of the angle I have.

I'm using SFML, and I'm using sf::Shapes for all. So to move the projectiles, I can either use the Move() function, that has X and Y parameters and move the sf::Shape with the value that I put on the parameters, or I can use SetPosition(), that also has X and Y values.
0

Share this post


Link to post
Share on other sites
Each bullet needs a velocity vector that looks something like (speed*cos(angle), speed*sin(angle)). You may have to tweak the formula slightly depending on the convention you used to specify the angle.

On a time step of length delta_t, you update the position like this:
[code]new_position.x = old_position.x + velocity.x * delta_t;
new_position.y = old_position.y + velocity.y * delta_t;[/code]

As usual, I recommend that you use a unit-length vector instead of an angle to begin with, and then you just need to multiply it by the speed. Edited by Álvaro
2

Share this post


Link to post
Share on other sites
The general update mechanism of a 2D point with constant velocity towards a given aimed angle looks something like
[code]
float newX = x + cos(angle) * velocity * deltaTime;
float newY = y + sin(angle) * velocity * deltaTime;
[/code]
The angle is expressed in radians, velocity is the speed of the projectiles, and deltaTime is the amount of time that has passed since the last update.
1

Share this post


Link to post
Share on other sites
[quote name='clb' timestamp='1352225977' post='4998136']
The general update mechanism of a 2D point with constant velocity towards a given aimed angle looks something like
[code]
float newX = x + cos(angle) * velocity * deltaTime;
float newY = y + sin(angle) * velocity * deltaTime;
[/code]
The angle is expressed in radians, velocity is the speed of the projectiles, and deltaTime is the amount of time that has passed since the last update.
[/quote]

Though it makes sense to only recalculate the velocity-vector when the input change, since that should be a lot less often then the framerate.
Not that it likely matters much. (and maybe it's animated, and need to change every frame)
Another nitpick: "velocity" is always a vector, and speed is a scalar (the length of the velocity vector), so it would look neater if it said "speed" in the code :) Edited by Olof Hedman
2

Share this post


Link to post
Share on other sites
Thank you all. I've had already tried to do with this formula, It doesn't work. But then I tried on another simplified code and it works perfectly, so there is something to do with the rest of my code, not the formula.

One more question. I've seen people saying do avoid moving things with angles, like Alvaro. Why is that so?
0

Share this post


Link to post
Share on other sites
[quote name='L i n c k' timestamp='1352292760' post='4998366']
One more question. I've seen people saying do avoid moving things with angles, like Alvaro. Why is that so?
[/quote]

Vectors are a more natural representation, and as a result many operations are easier to do with vectors than with angles.

If you use angles, you'll have to use sine and cosine all the time, precisely to do what you were trying to do in this thread. You are also likely to have to use atan2 to extract an angle from a vector at some point. These operations are very expensive, and at some point you realize that you don't need to go through the angle at all: Just keep a unit-length vector, which is effectively (cos(angle), sin(angle)). Rotating vectors is not too hard, especially if you are familiar with complex numbers, since multiplying by a unit-length complex number is precisely a 2D rotation.

A second reason to avoid angles is that the fact that 0 and 360 degrees are the same angle means that at some point you'll either have non-uniqueness in your representation or a discontinuity somewhere. It also means that figuring out if a direction is between two other directions is tricky, and so is interpolating between two directions.

A third reason is that angles are incredibly messy when you move to 3D. One can still represent a rotation as three angles (see Euler angles), but other representations (quaternions and orthogonal matrices) are much easier to work with (faster, better behavior when interpolating, no gimbal lock).

Getting rid of angles usually results in code that has fewer special cases, is more elegant, is easier to get right and runs faster. The only downside is that you have to educate yourself to think in terms of vectors and matrices instead of the more familiar angle. But it's totally worth it.
1

Share this post


Link to post
Share on other sites
Hmm, makes sense. Indeed I had some problems with this equality between 0 and 360.

I don't have any idea how it works using vectors for that. Do you have any link for a tutorial? Or can you tell me what to search for?
0

Share this post


Link to post
Share on other sites
I don't know of any learning materials for this. But if you tell me what operations you want to perform on your directions, I can tell you how to do them.

Are you familiar with complex-number arithmetic?
0

Share this post


Link to post
Share on other sites
Hmm, I don't have any task I need to do right now. I just wanted to learn so I can use in some future project. The game I was making is almost finished, I solved all my problems.
0

Share this post


Link to post
Share on other sites
I don't know how old you are or if you are still a student, but the best place to gain some solid foundation about these things is a Linear Algebra course, usually offered in the first year of college.
0

Share this post


Link to post
Share on other sites
I have an idea what complex numbers are, and I know there are some great videos on youtube about this, I actually watched some, but I don't remember anything about it now. I know they'll be there for when I want to learn =)

I'm not in college anymore, I'm 21. I'm 100% sure they didn't teach that. =|
0

Share this post


Link to post
Share on other sites
There is a guy at my work that told her daughter he would pay for her college education if and only if she took Linear Algebra and Statistics. Smart man... :)
0

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