Jump to content

  • Log In with Google      Sign In   
  • Create Account


Angular movement in SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 AlanSmithee   Members   -  Reputation: 953

Like
0Likes
Like

Posted 16 April 2012 - 09:08 AM

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 straight 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:

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

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

(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

Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2745

Like
0Likes
Like

Posted 16 April 2012 - 11:12 AM

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.

#3 Zael   Members   -  Reputation: 154

Like
0Likes
Like

Posted 16 April 2012 - 11:42 AM

I have to ask why not just use vector math?

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;

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.

#4 AlanSmithee   Members   -  Reputation: 953

Like
0Likes
Like

Posted 16 April 2012 - 01:21 PM

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.

#5 Álvaro   Crossbones+   -  Reputation: 11714

Like
0Likes
Like

Posted 16 April 2012 - 01:51 PM

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:
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;
}

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.

#6 AlanSmithee   Members   -  Reputation: 953

Like
0Likes
Like

Posted 16 April 2012 - 02:49 PM

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. Posted Image

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

Thanks! / AS.

#7 Trienco   Crossbones+   -  Reputation: 2041

Like
0Likes
Like

Posted 17 April 2012 - 10:06 PM

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.
f@dzhttp://festini.device-zero.de

#8 AlanSmithee   Members   -  Reputation: 953

Like
0Likes
Like

Posted 20 April 2012 - 07:13 AM

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 :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS