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.

Newcomers DBPro Corner / How do you get a 2D sprite to move at a chosen angle?

Author
Message
Lonnehart
15
Years of Service
User Offline
Joined: 17th Apr 2009
Location:
Posted: 6th Jun 2011 12:09
Is there a formula that lets you choose what angle you want objects to travel in? And curves as well? I don't want to predetermine a point offscreen then have the sprite to travel to it...

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 6th Jun 2011 12:43
Here's an unfinished project I started writing a couple of years ago. Don't worry about the asteroids but pay attention to the moveship() function. This uses sin and cos to move the ship at an angle but I think you can use the rotate sprite and move sprite commands as well (haven't tried). Good luck

A clever person solves a problem, a wise person avoids it - Albert Einstein

Attachments

Login to view attachments
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Jun 2011 23:28
atanfull() will point you in the right direction. And you can use curveangle for the interpolation.

Lonnehart
15
Years of Service
User Offline
Joined: 17th Apr 2009
Location:
Posted: 9th Jun 2011 06:18
So I can use curveangle to make my sprite curve towards a specific point. How about going straight? And how exactly do Cos and Sin work?

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th Jun 2011 02:42
The sin and cos in my example was actually from another program which I manipulated to work in my program although I didn't understand the maths behind it at the time. I've done a bit more playing around and basically, you can used the concept of projectile motion without gravity to move your sprite at a chosen angle.

In a nutshell:
x velocity = Vcos(angle) where v is your initial velocity (desired speed) and the angle is the direction you want your sprite to move in.

y velocity = Vsin(angle) same as above.

Attached an example of the above. Hope the comments make sense

If you want more info, google "Projectile Motion" and "Unit Circle"

A clever person solves a problem, a wise person avoids it - Albert Einstein

Attachments

Login to view attachments
Lonnehart
15
Years of Service
User Offline
Joined: 17th Apr 2009
Location:
Posted: 12th Jun 2011 12:01
Ah... so that's how the math works.

After much experimentation with the example given in the Help menu of DBpro, I figured out how the formula works. It goes like this...



Now all I have to do is stuff this into a function so I can use it to make every sprite move whever I want to (like enemy formations in most 2D Bullet Hell Shooters).

Thanks to everyone who gave me an assist on this. My brain was burning when I first tried to figure it out.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th Jun 2011 14:19
Glad it all makes sense. Nice to know that my explanations can be understood.

A clever person solves a problem, a wise person avoids it - Albert Einstein

Login to post a reply

Server time is: 2024-11-22 22:18:38
Your offset time is: 2024-11-22 22:18:38