Getting XY speed from an angle/velocity

macboy
Unregistered
 
Post: #1
I'm writing some code with a vehicle in it. I need to know how I can get the x and y speeds for it from the current angle and speed of the car. No gravity or other natural forces included.

Let's say the angle is 180 degrees (facing user/south) and the speed is 5. Everybody should know that this means that the y speed is 5 and no x speed. What I need to know is how to get this if it's a non-cardinal direction, such as 79 degrees.

Thanks in advance.

P.S. Use pseudo-code or BASIC if possible.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Basic trigonometry:

Code:
vx = v * cos(angle)
vy = v * sin(angle)
Quote this message in a reply
macboy
Unregistered
 
Post: #3
Quote:Originally posted by OneSadCookie
Basic trigonometry:

Code:
vx = v * cos(angle)
vy = v * sin(angle)
Heh. Thanks. Haven't got into trig much yet in school.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Then you should be aware that sin & cos functions almost always take angles in radians, not degrees.

To convert degrees to radians, divide by 180 and multiply by π.
Quote this message in a reply
Moderator
Posts: 680
Joined: 2002.11
Post: #5
METAL uses radians. -45 degrees = 0 radians.

BTW, I've had NO trig. And I still haven't been able to determine something's angle in relation to something else.

I'll just continue this thread. Say I have a player x, y, and angle. px, py, and pangle, respectively. Given ex and ey (enemy x, enemy y) how do I find out what angle the enemy is in relation to the player? I'm trying to make the AI turn towards powerups in my entry in the METAL contest and I can't figger this part out.

My web site - Games, music, Python stuff
Quote this message in a reply
macboy
Unregistered
 
Post: #6
Wait, the angle code doesn't work converting to radian or something.

It's moving to the right side (about +45 degrees)... here's my code:

[SOURCECODE]
xspeed = speed * cos((angle/180)*pi)
yspeed = speed * sin((angle/180)*pi)[/SOURCECODE]I'm using pi as 3.14.
Quote this message in a reply
Member
Posts: 20
Joined: 2002.12
Post: #7
Quote:Originally posted by diordna

how do I find out what angle the enemy is in relation to the player?

p1Y is the player's Y position
p1X is the player's X position
p2Y is the enemy's Y position
p2X is the enemy's X position
angle is the angle of the enemy in relation to the player in degrees

[sourcecode]
p1Y = 50
p1X = 50

p2Y = 25
p2X = 25

pi = atn(1)*4

if p1Y > p2Y then difY = p1Y-p2Y else difY = p2Y-p1Y
if p1X > p2X then difX = p1X-p2X else difY = p2X-p1X

angle = atn(difY/difX)*180/pi
[/sourcecode]

"Programmers are tools for converting caffeine into code."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
diordna -- atan2. This was covered a week or so ago by another METAL programmer.

macboy -- precisely whether you should use sin() or cos() for x will depend on your coordinate system (you'll always use the other for y). You may also want to change the sign of either x or y to make the angle go the other way.
Quote this message in a reply
macboy
Unregistered
 
Post: #9
Thanks everybody. Switching sin and cos then multiplying the y speed by -1 made it work.

Next dilemma, not the same, but it has to do with this project. How can I do some sort of drag physics, for instance, when you drive straight then turn right, how can I make the back end swerve when that happens?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
Treat the front & back wheels of the car as two different particles, each with their own velocity. When the user steers, change only the velocity of the front particle, leave the back one doing what it was. Each frame, move the particles by their velocity * dt, then move the particles towards each other so they are some constant distance apart. Apply acceleration only to the front wheel particle.

http://www.gamasutra.com/resource_guide/...n_01.shtml (free registration required) might be of some use to you.
Quote this message in a reply
macboy
Unregistered
 
Post: #11
Quote:Originally posted by OneSadCookie
Treat the front & back wheels of the car as two different particles, each with their own velocity. When the user steers, change only the velocity of the front particle, leave the back one doing what it was. Each frame, move the particles by their velocity * dt, then move the particles towards each other so they are some constant distance apart. Apply acceleration only to the front wheel particle.
Thanks.

Quote:Originally posted by OneSadCookie
http://www.gamasutra.com/resource_guide/...n_01.shtml (free registration required) might be of some use to you.
Actually I just pressed back and it worked Sneaky
Quote this message in a reply
macboy
Unregistered
 
Post: #12
New problem... this time I don't have a velocity but instead a relative ending point. I need to figure out how to make it an offset of the parent object.

In English:
I have the car's current position, speed, and velocity.
The car can shoot a projectile (which doesn't show up) which creates a crater where it "landed".
I need to know how to figure out the crater's position without creating a moving projectile sprite.

Here's the current code:
[SOURCECODE] craterx = playerx + (gunrange * sin((angle/180)*pi)) + random(gunrandoffset*2)-gunrandoffset
cratery = playery + (gunrange * cos((angle/180)*pi)) + random(gunrandoffset*2)-gunrandoffset[/SOURCECODE]
But unfortunately this makes it start from the player's top-left corner, while the gun is in the middle.

Sorry if that didn't make much sense Shock
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
craterx = playerx + playerwidth/2 + ...
cratery = playery + playerheight/2 + ...

?
Quote this message in a reply
macboy
Unregistered
 
Post: #14
Sorry. Doesn't work because if the player turns around so it's 180 degrees it will be to the left of the player Ninja
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #15
So why not make playerx and playery the center of the player? That's usually more useful anyway...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Formula for converting angle to vector? komirad 2 9,831 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  OpenGL Velocity and Grouping of Lines (C++, Xcode) TheThirdL3g 2 3,240 Jul 29, 2010 01:28 PM
Last Post: SethWillits
  Question Regarding the Reflect Angle of a Transition iBaby 3 3,503 Apr 27, 2010 03:15 PM
Last Post: JustinFic
  ending location from angle and speed Kazooless 5 4,473 Apr 3, 2009 02:40 PM
Last Post: Gillissie
  Angle between two points? Graphic Ace 6 5,353 Nov 8, 2008 12:11 PM
Last Post: macnib