Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 June, 2013, 03:52:05 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Scaling Vectors Help -  (Read 378 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Emerov
LV1 Newcomer (Next: 20)
*
Offline Offline

Gender: Male
Last Login: 01 April, 2013, 06:14:25
Date Registered: 07 January, 2012, 20:33:05
Location: Phoenix, Arizona
Posts: 11

Topic starter
Total Post Ratings: 0

View Profile
« on: 05 June, 2012, 03:40:29 »
0

OK, so I have a golf-ish game where there is a ball that moves around the screen with separate x and y velocities which vary based on an angle chosen before the ball is hit. This all works perfectly fine, the ball bounces off the walls, etc. But when I try to apply friction to the ball's movement, I encounter some issues.

The process I am using to scale down my velocity vector while maintaining the same angle of motion is as follows:
   1. Find the magnitude of the vector
   2. Normalize the vector
   3. Decrease the value of the magnitude by a friction constant
   4. Multiply x and y velocity components by the new magnitude
My code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.PUTTPUTT
P*16**cos(θ)->V             //P represents power, used to determine how fast the ball will move
P*16**sin(θ)->W             //V is the X-velocity; W is the Y-velocity
Repeat getKey(15)
 Pt-On(X+V->X/256, Y+W->Y/256, Pic0)     //X and Y are the position components
 127*W//sin(tan-1(V,W))->M               //M is the magnitude of the velocity vector
 M-F*W//M->W                //F is the friction constant. I multiply the new magnitude by each component before dividing by the current magnitude
 M-F*V//M->V                //because W//M would give a value of 0
 If X/256>90
  -V->V
 End
 If Y/256>59
  -W->W
 End
 DispGraphClrDraw
End

Depending on P and θ, the code may or may not work, and I can't figure out why; the ball does seemingly random stuff, often not even going in the right direction. Huh?
Also, I don't know if this is relevant, but when I debugged the magnitude, it changed whenever the ball bounced off the left or right of the screen, which I don't think should be happening.
Can anybody help me fix this?
« Last Edit: 06 June, 2012, 22:55:24 by Emerov » Logged
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 17:53:05
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1849


Total Post Ratings: +480

View Profile
« Reply #1 on: 05 June, 2012, 03:56:18 »
0

I took a cursory glance at your code and didn't find anything wrong with your math or concept. While there may be an issue there from your description of the problem I suspect that the main culprit here is overflow. The main computation register in Axe can only hold a value smaller than 65536 at any one time, and W^2+V^2 very likely goes over that especially if you are using x256 precision.

Maybe you can scale the x and y components by the friction constant directly instead of using the magnitude?
« Last Edit: 05 June, 2012, 03:57:20 by squidgetx » Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
MGOS
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Yesterday at 20:43:31
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297


Total Post Ratings: +74

View Profile
« Reply #2 on: 05 June, 2012, 10:48:18 »
0

Also, make sure to add the parentheses in the square root bracket, because axe will compute it like this:

1
2
3
4
(W²+V)²
Do this instead:
W²+(V²)
« Last Edit: 05 June, 2012, 10:48:57 by MGOS » Logged

Click those to see more information Smiley
Current   
Projects: 
Hayleia
Programming Absol
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Last Login: Yesterday at 21:21:11
Date Registered: 01 June, 2011, 20:12:47
Location: ud-ud ?
Posts: 2119


Total Post Ratings: +264

View Profile
« Reply #3 on: 05 June, 2012, 18:19:18 »
0

Also, make sure to add the parentheses in the square root bracket, because axe will compute it like this:

1
2
3
4
(W²+V)²
Do this instead:
W²+(V²)
^This.
Also, make sure you used "²" which is the square operator, and not ^2 which is a modulo operator Wink
Logged





Spoiler for what I am according to...:
me: useless
Pokemon Test: an Absol
turiqwalrus: an eggplant
p2: A HUMAN BEING !
Blackpilar and p2: iplantonlyplantwantplanttoplantknowplantifplantyouplantareplantaplantboyplantorplantaplantgirlplant
click here to know where you got your last +1s
MGOS
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Yesterday at 20:43:31
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297


Total Post Ratings: +74

View Profile
« Reply #4 on: 05 June, 2012, 19:02:08 »
0

Also, make sure you used "²" which is the square operator, and not ^2 which is a modulo operator Wink
Oh, I totally overlooked that. Shocked
Logged

Click those to see more information Smiley
Current   
Projects: 
Emerov
LV1 Newcomer (Next: 20)
*
Offline Offline

Gender: Male
Last Login: 01 April, 2013, 06:14:25
Date Registered: 07 January, 2012, 20:33:05
Location: Phoenix, Arizona
Posts: 11

Topic starter
Total Post Ratings: 0

View Profile
« Reply #5 on: 06 June, 2012, 22:53:44 »
0

@Hayleia and MGOS: First off, I wasn't using the mod operator, but the superscript tags didn't seem to work within code tags. Also, the order of operations thing did mess up my code, but I had to scrap this line because of potential overflow.

@Squidgetx: To prevent overflow, I went ahead and used trig to find the magnitude of the vector (I do need the magnitude, because normalizing the vector to scale it should be a bit more precise than using a ratio to estimate the new component values individually).

So now the problem with the magnitude changing when the ball bounces is fixed, but the ball still moves in directions seemingly unrelated to the assigned angle of motion. I think it has to do with the lines where I scale the components (M-F*W//M->W), because when I comment them out, the code works perfectly except, of course, the ball doesn't stop. I just cannot figure out what I'm doing wrong. Tongue
Any more suggestions?
Logged
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 17:53:05
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1849


Total Post Ratings: +480

View Profile
« Reply #6 on: 07 June, 2012, 02:07:17 »
0

How does it move? Does the angle seem wacky right at the start or is it only when it bounces?

Edit: Also, why are you using the fixed point multiplication to assign V and W? Couldn't you just use regular multiplication and divide by 128?
« Last Edit: 07 June, 2012, 02:09:09 by squidgetx » Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.381 seconds with 30 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.