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 use angle measures in 2d?

Author
Message
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 6th Jan 2006 00:37
How do you use angle measures in 2d? Basicly what I want is to determine what angle the mouse is from the center of the screen, and shoot a "bullet" sprite from the center to the mouse pointer.
How?

Read Ender's Game. And all its sequals.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 6th Jan 2006 01:51
Use the ATANFULL command.


Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 6th Jan 2006 02:21 Edited at: 6th Jan 2006 02:23
ok ill try it. Im using Darkbasic Classic just so everyone knows. It says its an unknown command.

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 6th Jan 2006 02:27
ok that works, but how do i make a sprite move towards my mouse? (from the center?)

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 6th Jan 2006 03:36
sync rate 0


circle 2,2,2
get image 1,0,0,5,5

ink rgb(255,0,0),0


sprite 1,x,y,1
bullet=0
sync
x=320
y=240

do
sprite 1,x,y,1
if mouseclick() and bullet=0 then xx=mousex() : yy=mousey() : cls : line 320,240,xx,yy : bullet=1
if bullet = 1
if point(x-1,y)=rgb(255,0,0) then dec x else inc pr
if point(x-1,y-1)=rgb(255,0,0) then dec x : dec y else inc pr
if point(x,y-1)=rgb(255,0,0) then dec y else inc pr

endif
if pr=3 then x=320 : y=240 : bullet=0
pr=0
sync

loop

























Weird color specific tracking idea. Click in the upper left quadrant and the "bullet" will follow the trail. it works for now but i dont like the line....i think ill just make the ground (this is a top view shooter thing) the color of the line.

Read Ender's Game. And all its sequals.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 6th Jan 2006 18:21
ATANFUL!? Are you *#%@ing kidding me!

I spent this entire week working on some huge mathematical equation with COS and Sin and a bunch of other crud to do the exact same thing, and now I see the Atanful command lol!

*sigh*, guess I should've read the Index first.

Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 7th Jan 2006 00:17
lol! Hey RUCCUS, I have the means of shooting a bullet from the middle of the screen to my mouse right now, but my way dosnt really cut it. Do you have another idea? I dont know how to use the atanful command in this way.

OK so i know the angle my mouse is at, but how do i get a sprite to move at that angle?

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 7th Jan 2006 01:04
OK, that question was not for RUCCUS alone! Anyone else want to try to answer?

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 7th Jan 2006 03:23
Come on! are you affraid to be right?! Or even close!

Read Ender's Game. And all its sequals.
Cookyzue
21
Years of Service
User Offline
Joined: 12th Oct 2003
Location: Who Cares?!
Posted: 7th Jan 2006 03:33
Rotate the sprite to the angle you get from atanfull then move it.


MCK Games
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 7th Jan 2006 18:18 Edited at: 7th Jan 2006 18:31
After experimenting with the Atanful I've realized it isnt what I need afterall Though it did help, Im back at using Acos and Asin commands; I should have a function up and running soon I just need to solve one last problem.

The reason it didnt work for me was because I needed information in 3 dimensions and some other mumbojumbo, but for what you need Atanfull does sound like it should work, try what was suggested above.

<EDIT>

To do the above suggestion I think you'd:

- Store two positions, in this case one position is your Mouse's position, and the other is the sprites position. Lets say the mouses x is Mx#, the mouse's y is My#, the players ix is Px#, and the players y is Py#

So;

I'm assuming you're using sprites and the sprite shooting is SPRITE 1, change it to suit your needs.

Now that you have the two points you'd use a little formula to find the distance between the X positions and the distance between the Y positions, subtraction will do:

Now plug in the variables into the ATANFULL command;

And now you know the angle you have to shoot the bullets, how you determine to actually shoot the bullets is a different story though...

- Probably the simplest way would be to rotate the bullet sprite to the Angle# value, then use the MOVE SPRITE command to move it forward; you'd have to include some sort of If statement in there to check if the sprite is already in motion and if so not to rotate it or else the sprites would be rotating as you move the mouse, not too hard I'd imagine.

Im pretty sure thats how the Atanfull command works, never really used it before for a practical purpose but try it out and tell me how it goes.

Goodluck,
- RUC'

Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 8th Jan 2006 02:03
OK yeah, that would work fine for determineing the angle, but how do I move a sprite down that angle?

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 8th Jan 2006 02:42
Comon! HOW DO YOU MOVE A SPRITE AT A CERTAIN ANGLE!?

Read Ender's Game. And all its sequals.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 8th Jan 2006 04:42
I said how; now that you have the angle, you use the ROTATE SPRITE command to rotate the bullet sprite, then use the MOVE SPRITE command to move the sprite forward, the MOVE SPRITE command will move the sprite in the direction it's facing.

Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 8th Jan 2006 05:15
Quote: "ok ill try it. Im using Darkbasic Classic just so everyone knows. It says its an unknown command."


Read Ender's Game. And all its sequals.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 8th Jan 2006 05:23
What command is unkown the Atanfull command?

Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 8th Jan 2006 05:42
DBC dosnt have move sprite or rotate sprite...

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 8th Jan 2006 23:12
bump

Read Ender's Game. And all its sequals.
UFO
19
Years of Service
User Offline
Joined: 11th Oct 2005
Location:
Posted: 8th Jan 2006 23:15
Well, we didn't know because you didn't tell us.

You could use textured plains instead of sprites, which makes it alot easier in alot of ways.


Hippopotomonstrosesquippedaliophobia-Fear of long words
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 8th Jan 2006 23:28
Actually I did tell ya!

Hmm yeah I have some code that uses a plain....but im not very good in 3D. Ill look into it.

Read Ender's Game. And all its sequals.
UFO
19
Years of Service
User Offline
Joined: 11th Oct 2005
Location:
Posted: 8th Jan 2006 23:48
Oh yea, on the third post...sorry.


Hippopotomonstrosesquippedaliophobia-Fear of long words
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 9th Jan 2006 00:15 Edited at: 9th Jan 2006 00:15
I cant seem to get my object to strafe. (WASD) This is what i have so far. It just rotates a plain. (I didnt make it, Zergei did)

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 9th Jan 2006 00:49
using this code for plain rotation, how do I strafe? (or even move)


set display mode 1024,768,16
make object plain 1,2,2
color object 1,rgb(255,255,255)

position object 1,x,y,z
sync on
sync rate 0
position camera 0,0,-50
do
set cursor 0,0
c1= mousex()-screen width()/2
c2= screen height()/2- mousey()
h#=sqrt(c1^2+c2^2)
print c1," ",c2," ",a#," ",object angle z(1)


a#=acos(c1/h#)
if c2<0 then a#=a#*-1

zrotate object 1,wrapvalue(a#)





sync
loop

Read Ender's Game. And all its sequals.
Milkman
18
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 9th Jan 2006 01:57 Edited at: 9th Jan 2006 02:23
once you get the angle, to move a sprite forward at that angle position it like so:


that should be correct, but havent tested it...
EDIT:
its actually like this, sorry:

i had the sine and cosines reversed

or you could do it like this:


although i think the newxvalue() and newzvalue() functions only exist in dbpro, not dbc...

"Genius is 1% inspiration and 99% perspiration"
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 9th Jan 2006 02:48
yeah, i dont think position sprite is either...

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 11th Jan 2006 05:40
OK, to put it simple.

All I want is to detect a sprite inbetween point 320,240 and point mousex(),mousey().

Read Ender's Game. And all its sequals.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 11th Jan 2006 05:40
Can it be done?

Read Ender's Game. And all its sequals.
Milkman
18
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 11th Jan 2006 06:19
Quote: "yeah, i dont think position sprite is either..."


paste sprite would work just the same...

Quote: "All I want is to detect a sprite inbetween point 320,240 and point mousex(),mousey(). Can it be done?"


don't know, i havent worked with sprites much, sorry.

"Genius is 1% inspiration and 99% perspiration"
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Jan 2006 06:47
Quote: "OK, to put it simple.

All I want is to detect a sprite inbetween point 320,240 and point mousex(),mousey().

Can it be done?
"


Probably the easiest way is to make the bullet a sprite and do simple sprite collision while moving the bullet sprite towards the mouse position.


Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 13th Jan 2006 03:51
Quote: "Probably the easiest way is to make the bullet a sprite and do simple sprite collision while moving the bullet sprite towards the mouse position."

Yeah I know!!!!!! I dont know how!

Read Ender's Game. And all its sequals.
Bluestar4
18
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 13th Jan 2006 12:54
I did my project missle might in all 2d, and had to make my own movement engine to move the bullets because there were no automatic commands to move the sprites from point A to point B in dbc. and yes, I even had to come up with a function to figure the exact angles before I could even do this.
IT can be done however using 3d objects very simply, as in you make a couple objects, the first object would ofcourse be centered on the screen, the second object would be hidden and the possition of your mouse x and y. you would then use the point object command to point the one in the center of the screen to the object where the mouse is.
you can then use the move object commmand to move the showing object to the hidden one. note however, that using this method, it wont be all 2d. now if you want to continue to do this in 2d you need to figure the bearing from point a :

now that you have the bearing using the code above {which ofcourse can be modified to suit your programs needs ) you can start to work on your movement engine which may look something like this :



the parameters of my simple function above is spritenum = this is the sprite number you wish to move,
destx = this is the x =coordinate of where you wish it to move to,
dexty = same as above only y cooridnate,
the mspeed parameter is what controls the speed of the missles in this function,1 being the fastest,
local variablae g is simply the bearing or angle in degrees from 0 to 359.
as you can see from the code, an angle of 0 will move the sprite straight up on the screen and an angle of 180 will move the sprite straight down. (90 and 270 would likewise go completely right or left respectively )
as you can see from the code , coding using the 3d objects might be more simpler

Bluestar4~
code master
20
Years of Service
User Offline
Joined: 4th Dec 2003
Location: Illinois
Posted: 13th Jan 2006 23:11
Quote: "All I want is to detect a sprite inbetween point 320,240 and point mousex(),mousey()."


Thats easy, just use this:


later

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 14th Jan 2006 00:16
Quote: "Yeah I know!!!!!! I dont know how!"


Just like what was said above use "atanful" to get the angle and use "sin" and "cos" to get the amounts you need to add/subtract from the center x and y coordinates.

I made this code snip for you... it should be everything you want with plenty of rem statements to tell you whats going on.

This should work in both Classic and Pro:



Bluestar4
18
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 14th Jan 2006 10:02 Edited at: 14th Jan 2006 10:52
here is the basics of what you posted :


using your method you will find that the angle returned is not corrrect but will return a number that is a full 180 degrees out ( which is why I posted the code to get the correct angle and placed it in a function so it could be used in anyones project) you will note that when the code below is ran that directly up on the screen is now returned correctly at 0 degrees, and all the way to the right is also correctly printed at 90 degrees and to the left 270 , ect,

Since the function is accurate on the angles it can also be modified
so that you can make firing arcs for your ships and objects that actually work

Bluestar4~
Bluestar4
18
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 14th Jan 2006 14:17
also in addition to my previous post, I noticed another small mistake..
in order to move the sprite to ANY angle you should note that you were on the right track, but made one small math error .. the + on the right side of the code should also be a - . the following code will move your sprite toward ANY ANGLE but assumes 0 degrees is located at 320,0 x and y
and that 90 degrees is directly right , and 270 is directly left,ect
all you need to move it in the direction you want it is the angle which you can get from the function I have provided OR simply put the angle in that you want it to go :



now, since you have all been racking your brains out trying to actualy understand what I have been saying and if your still reading at this point here is your reward from me ... a fully functional basic example of what I have been trying to teach you is below :


and now that you know , please do have fun programming your games and email me if you have actually read and found this helpful to solving your movement probs.

Bluestar4~
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 21st Jan 2006 01:29
OK. These last posts were helpful. Ill report back soon.

Read Ender's Game. And all its sequals.
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 23rd Jan 2006 18:50
that's very nice bluestar4.

life's one big game
Shadowed Lightning
20
Years of Service
User Offline
Joined: 10th Nov 2004
Location:
Posted: 24th Jan 2006 02:06
I used rotating sprites with atanfull and moving them to mouse and a bunch of stuff like what you need in my 2D RTS so let me know if u still need help cause i might be able to.

AMD Athlon 64 3200 - GeForce 6800 GT - 320GB Hard Drive
1024MB RAM

Login to post a reply

Server time is: 2024-11-27 05:31:07
Your offset time is: 2024-11-27 05:31:07