Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Diagonal Sprite Movment

Author
Message
kkun
14
Years of Service
User Offline
Joined: 10th Jun 2009
Location:
Posted: 10th Jun 2009 10:53
Hey guys i'm new to the darkBasic scene and would like to be directed in how to program

a sprite moving in one way but would aim where ever the buttons are

eg if im moving up and i shoot down he'd face down while moving up.

a good example would be smash tv on the super nintendo.

i'm having problems with the diagonal shooting mainly. So if i hit left and down it'd shoot bottom left.
chwilly
15
Years of Service
User Offline
Joined: 23rd Feb 2009
Location:
Posted: 11th Jun 2009 07:34
Think of
Y increment: UP as -1, DOWN as 1
X increment: LEFT as -1, RIGHT as 1

It doesn't matter what direction he is facing (each must be treated individually), if he is shooting down then Y increment = 1 and X increment = 0

So bottom(down)/left is: Y increment = 1 and X increment = -1
You use movement the same way

This is how I use it anyways
kkun
14
Years of Service
User Offline
Joined: 10th Jun 2009
Location:
Posted: 11th Jun 2009 19:20
Ah i see, i didnt actually think of that and assumed there was some crazy sin cos or tan formula associated with it.
thanks for that.
NeX the Fairly Fast Ferret
19
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 13th Jun 2009 14:06
Well, actually, no...

Movement along each axis when moving diagonally should be 0.70710678118654752440084436210485x what it is along a single axis when moving horizontally or vertically. sin( 45 ). Try it.

chwilly
15
Years of Service
User Offline
Joined: 23rd Feb 2009
Location:
Posted: 19th Jun 2009 08:16
What the hell are you talking about? Thats about 70710678118654752440084436210485 times more complicating then it needs to be.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 20th Jun 2009 00:11
What he's saying is that if you take an object moving at, say, 10 units per frame along either the x or y and switch it to move 10 units along a 45 degree diagonal you have to change the amount it's moved along the x and y axis by a factor of roughly .7071067. Otherwise it moves faster on the diagonal than it does on the axes. If you continued to move the object 10 upf on both x and y the diagonal speed would be sqrt (200) = 14.14 units per frame.

You should always break the x and y components of the velocity into separate values with x being

velocity * cos(angle)

and y being

velocity * sin(angle)

This likely will give you non-integer values so it's best to use floats for maintaining position and convert them to integer values when passing them to the positioning functions.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
chwilly
15
Years of Service
User Offline
Joined: 23rd Feb 2009
Location:
Posted: 20th Jun 2009 01:42
Pythagoras ez da bomb dude!

Anyway, your talking about regulating speed/velocity not direction. In a 2d sense, moving diagonal on the X/Y plane, say up and to the left, you are still just manipulating X/Y coordinates of the sprite reqardless of how you calculate speed, which is what I believe the question was. From what kkun was asking it appeared he was trying to figure out how the coordinate system works on the screen.

Login to post a reply

Server time is: 2024-04-27 01:26:27
Your offset time is: 2024-04-27 01:26:27