• 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
AlanSmithee

Angular movement in SDL

7 posts in this topic

Hello.

I am experiencing some problems with angular movement in SDL.

The Projectile is supposed to travel from static point A to static point B in a [i]straight [/i]line.
The problem is that the angular movement seem to be snapped to whole 45 degrees.

To explain the behaviour that I am experiencing:
If there is a point A at 0,0 (x, y) that should travel to point b at 200,10 (x, y) then the object first travels 190 pixels in the x axel to 190,0 and then travels at a 45 degree angle in the posetive x, y angle.
191,1
192,2
193,3...
to
200,10

Now this isn't a very projectile like behaviour and unwanted.

My code looks like:

[CODE]float radians = atan2(b.y - c.y, b.x - c.x);

c.x += 4 * std::cos(radians);
c.y += 4 * std::sin(radians);[/CODE]

(c being the traveling object and b the goal)

What is even more wierd is that if I change the speed modifier (represented by the litteral 4 in the above code) the behaviour of the moving object differs. It seems like the faster the object is travelling the more steep angles it can move in.
At 2 speed it can only move in 45 degree angles, at 4 speed it can move in 22.5 degree angles etc.

I would be very thankful for any insight as to what might be causing this problem.

Thanks in advance. / AS
0

Share this post


Link to post
Share on other sites
If I had to hazard a guess, because I can't run the code from where I am, I'd guess that when you are multiplying the speed times the cosine and sine, it frequently gets truncated to zero. For instance

2 * sin(x) for all real numbers x from -3.14159 to 3.14159, almost half of the results of the expression will be less than one, and be truncated to zero, then almost half would be greater than or equal to one, and less than two. If floating point inaccuracy allows, then you might get a two, very infrequently.

This may be a source of the problem, where if you increase the speed to four, then you have more precision, as more values can be attained.
0

Share this post


Link to post
Share on other sites
I have to ask why not just use vector math?

[CODE]
float unitVectorX = b.x-c.x;
float unitVectorY = b.y-c.y;
float distance = sqrt(unitVectorX*unitVectorX + unitVectorY*unitVectorY);
unitVectorX /= distance; //results in amount of x to travel per unit
unitVectorY /= distance; //result in amount of y to travel per unit
c.x += unitVectorX*velocity;
c.y += unitVectorY*velocity;
[/CODE]

Maybe just my opinion, but I find it far simpler to think about. I could be wrong, but I also believe it is significantly faster than having to use the trig functions.
0

Share this post


Link to post
Share on other sites
Hello!

Ectara - you were 100% correct, thank you very much!
In my frustration I neglacted something so basic as that decimals are lost in float to int convertion.

Zael - Thanks for your answer. I'm sorry but I can't give you any answer as to why I dont use vector math, other then that I dont know it. I also dont know wheter or not there are any significant performance gains by using either method. I am not very comfortable with trig or vector math or any math at all tbh, so for the time being until I have had a chance to get some deeper knowledge I'll stick with using trig for this and rotation and such since I, with the help of friends, have gained the most basic knowledge of it.

TY again both of you / AS.
0

Share this post


Link to post
Share on other sites
You can easily wean yourself off angles by storing a unit-length vector in any situation where you are currently using an angle. The coordinates of this vector are (cos(a), sin(a)).

For the code you just described, the sine and cosine are already computed, so you don't need to do anything.

Angle addition has a funny formula when you represent both angles as unit vectors:
[code]Vector compose_rotation(Vector v, Vector w) {
Vector result;
result.x = v.x*w.x - v.y*w.y;
result.y = v.x*w.y + v.y*w.x;
return result;
}[/code]

Incidentally, that is the formula for multiplying the complex numbers (v.x+i*v.y) and (w.x+i*w.y), which is why thinking of rotations as unit-length complex numbers is very handy. It also prepares you for computing composition of 3D rotations as multiplication of unit-length quaternions.

If you need help implementing anything else using vectors, feel free to ask.
0

Share this post


Link to post
Share on other sites
Hello Alvaro!

Actually, me and my friend were just discussing this as I brought up that someone suggested using vector math for angular movement ets and he said the same thing as you, so I think I'll take my timer sooner rather then later to learn it. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

The plan is ofcourse to move to 3d eventually so might as well get as used as I can right now!

Thanks! / AS.
0

Share this post


Link to post
Share on other sites
You really should. Because honestly, saying you want to move in a _straight_ line and then bringing angles and expensive trig functions into it is slightly insane. Why would there be angles, if you move straight? Also, I'm pretty sure "Angular movement" doesn't mean what you think it means, since at no point in your scenario are there any changing angles.
0

Share this post


Link to post
Share on other sites
Hi Trienco.

Yeah, as I said im not too familiar with math in general, so I might missword something and get different techniques mixed up.

I've changed it accordingly now and it works rather well :)
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