123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|27|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Limiting vx and vy movement?

Sat, 26 Oct 2019, 01:32
Pakz

Limiting vx and vy movement?


I spend quite some time looking for this in google last night. I wanted to create a spaceship that moves like the one in asteroids. Though I was not able to find a vector like function for radians that limits the x and y movement speed. (I have a angle in radians also)
I remember trying to understand this from a example this year but no idea where this was.

What I did as a fix was to use a for loop that adds to the vx and vy and exits if one of these gets over the maxspeed. This seemed to work but would anyone know the formula for this?

I thing it is some kind vector length function that I need?

edit : I am sporting atm so have some thinking time. I am thinking that if I add up the vx and vy and then divide them by 100 I could then use these percentage values to create new ones using the max moving speed divided by 100. This would keep the vx and vy inside the max movement limits.
Sat, 26 Oct 2019, 03:45
HoboBen
Are you thinking.of the magnitude of a vector?

This is pythagoras so sqrt(vx^2 + vy^2)

-=-=-
blog | work | code | more code
Sat, 26 Oct 2019, 03:55
Jayenkai
Yeah, like Hobo says, you need the Pyth distance as well as the angle.

newvx=vx+thrustx
newvy=vy+thrusty

Find the angle and distance of those, using atan and pythagoras, then limit the distance, then throw it all back together again with sin and cos, to get the true vx and vy.

-=-=-
''Load, Next List!''
Sat, 26 Oct 2019, 03:57
Pakz
I want to keep the movement speed(vx and vy) in bounds. So that is the magnitude?

I am so bad at this. I wil study later and see if I can turn the sqrt line into code.

edit: jay posted
edit2: brain hurts....

So I need to create the new vx and vy when thrusting(keydown) then find the angle of vx and vy, then find the distance of that and if it is to big recreate the vx and vy with that angle at the max thrust. Leaving the vx and vy alone if it is within thrust bounds. hmmm
Sat, 26 Oct 2019, 04:24
Jayenkai
Aaah, the joys of maths

Related : Shroom's Trigonometry Article

-=-=-
''Load, Next List!''
Sat, 26 Oct 2019, 04:29
Pakz
I was just looking at the nature of code by daniel shiffman. Wil look at the link when done rowing
Sat, 26 Oct 2019, 05:31
Pakz
I think this works. The ship seems to have the correct behaviour. If someone sees a mistake then ....



I wanted to quickly code a space game last night but my experience got to me and I got almost nothing done. Now I am going to try and lock the spaceship to the center and add a tile map with a planetoid cave system.
Sat, 26 Oct 2019, 05:41
Jayenkai
..
I think that looks right!

The truth is in the gameplay. Does it "Feel" right!?

-=-=-
''Load, Next List!''
Sat, 26 Oct 2019, 05:45
Pakz
Here the emscripten. I think it is ok?

cromdesi.home.xs4all.nl/emscripten/spaceshipasteroids/Main.html
Sat, 26 Oct 2019, 05:59
Jayenkai
Aye