Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411520 Posts in 69380 Topics- by 58436 Members - Latest Member: GlitchyPSI

May 01, 2024, 02:28:41 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Rotational movement Math problem
Pages: [1]
Print
Author Topic: Rotational movement Math problem  (Read 1973 times)
EmptyNull
Level 0
*


View Profile
« on: July 16, 2010, 04:21:59 AM »

The movement in a flash game I'm developing is like the movement in Asteroids (360 degrees) but then frictionless.

I rotate the ship with the left and right keys.

And I update the ship's motion when I hit the forward key with this code.
Code:
xMotion+=Math.cos(shipDirection*Math.PI/180);
yMotion+=Math.sin(shipDirection*Math.PI/180);

Now the problem I'm having is that while the movement is frictionless I still wan't to have a hard cap on it. Ie that the ship's motion doesn't go faster then a certain value in any direction.

I'm not really that good with math and the possible solution's I've tried to get this kind of effect have failed me.

So here I am asking for advice. Oh and If I'm not mistaken I think tan or atan is possibly needed for it as well.
Logged
raigan
Level 5
*****


View Profile
« Reply #1 on: July 16, 2010, 04:53:57 AM »

Code:
//(xMotion,yMotion) is the movement vector

len = sqrt(xMotion*xMotion + yMotion*yMotion) //len is the length of the movement vector

//todo: you probably want to check to avoid div-by-0 here

xMotion = xMotion / len;//now the movement vector is unit length
yMotion = yMotion / len;

clamped_len = math.min(max_speed, len);//limit the player's speed to be no greater than max_speed

xMotion = xMotion * clamped_len;//now the movement vector has length = clamped_len
yMotion = yMotion * clamped_len;


You might also want to check out Tonypa's tutorial about vectors: http://www.tonypa.pri.ee/vectors/start.html
 

Logged
bateleur
Level 10
*****



View Profile
« Reply #2 on: July 16, 2010, 08:27:32 AM »

Now the problem I'm having is that while the movement is frictionless I still wan't to have a hard cap on it. Ie that the ship's motion doesn't go faster then a certain value in any direction.

A quick and easy way to get this effect is as follows:

Code:
 import flash.geom.Point;

 var tmpPoint:Point = new Point();

 // Then during each frame calculation...
 tmpPoint.x = dx;
 tmpPoint.y = dy;
 tmpPoint.normalize(maximum_speed);
 dx = tmpPoint.x;
 dy = tmpPoint.y;

This works well provided your top speed isn't too high relative to the acceleration of the ship (which is typically isn't for Asteroids-like games).
Logged

muku
Level 10
*****


View Profile
« Reply #3 on: July 16, 2010, 11:26:13 PM »

A quick and easy way to get this effect is as follows:
[...]

I'm not sure what dx and dy are (velocity presumably?), but wouldn't that give you constant velocity instead of capped velocity?
Logged
bateleur
Level 10
*****



View Profile
« Reply #4 on: July 17, 2010, 12:51:44 AM »

I'm not sure what dx and dy are (velocity presumably?), but wouldn't that give you constant velocity instead of capped velocity?
Good point! Although it's essentially the same problem. To get capped velocity you just add an "if (tmpPoint.length > maximum_speed)".
Logged

raigan
Level 5
*****


View Profile
« Reply #5 on: July 17, 2010, 08:43:04 AM »

is my reply invisible or something?! WTF
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #6 on: July 17, 2010, 02:18:38 PM »

is my reply invisible or something?! WTF
Man, if I had a pound for every time I'd wondered that.
Logged
EmptyNull
Level 0
*


View Profile
« Reply #7 on: July 18, 2010, 05:21:57 AM »

is my reply invisible or something?! WTF

Ah sorry about that Raigan.

Your explanation did the trick. Hats off for you  Gentleman

I was busy browsing the man baby forum and didn't want to leave it yet.
Logged
bateleur
Level 10
*****



View Profile
« Reply #8 on: July 18, 2010, 06:23:15 AM »

is my reply invisible or something?! WTF
Oops, sorry!

I didn't notice it because it looked like a blockquote followed by a hyperlink to a comprehensive reference. So I thought an actual code example might be some use. Facepalm
Logged

raigan
Level 5
*****


View Profile
« Reply #9 on: July 19, 2010, 06:19:36 AM »

np, i just thought i was crazy for a moment  Grin
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic