﻿ Scaling Vectors Help
22 May, 2013, 22:22:21
 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
 Author Topic: Scaling Vectors Help -  (Read 370 times) 0 Members and 1 Guest are viewing this topic.
Emerov
LV1 Newcomer (Next: 20)

Offline

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

 « 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:
 12345678910111213141516 .PUTTPUTTP*16**cos(θ)->V             //P represents power, used to determine how fast the ball will moveP*16**sin(θ)->W             //V is the X-velocity; W is the Y-velocityRepeat 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 DispGraphClrDrawEnd

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

Gender:
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1831

Total Post Ratings: +477

 « 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

Offline

Gender:
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 295

Total Post Ratings: +73

 « 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:
 « Last Edit: 05 June, 2012, 10:48:57 by MGOS » Logged

Current
Projects:
Hayleia
Programming Absol
LV11 Super Veteran (Next: 3000)

Offline

Date Registered: 01 June, 2011, 20:12:47
Location: ud-ud ?
Posts: 2053

Total Post Ratings: +256

 « 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:
^This.
Also, make sure you used "²" which is the square operator, and not ^2 which is a modulo operator
 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
MGOS

Offline

Gender:
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 295

Total Post Ratings: +73

 « 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
Oh, I totally overlooked that.
 Logged

Current
Projects:
Emerov
LV1 Newcomer (Next: 20)

Offline

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

 « 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.
Any more suggestions?
 Logged
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)

Offline

Gender:
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1831

Total Post Ratings: +477

 « 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