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.

AppGameKit Classic Chat / problem using ATanFull with SetViewOffset

Author
Message
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 6th Feb 2013 04:43
Hello,

I am using ATanFull() to successfully find the angle from my pointer to the player sprite, and cause the player sprite to point directly at the pointer by using the following code



Works PERFECT...

However as soon as I start with the screen moving using SetViewOffset() .. well now it only works near the center of the screen.

Now I understand conceptually this is probably because the X/Y cordinates of either the screen, or the pointer, or the sprite are not going to match the code above.

But every way I have tried to accommodate for this does not seem to make a darn bit of difference?

So my question is once you start using SetViewOffset() how would I go about this?

See some failed examples here:


FAIL 1:



I also tried inverting the + and - on each line in case I had it backwards, no luck.

I also tried this and a few variants



Again I inverted all the + and - signs in case I had it backwards. Also no luck.

So any help appreciated, the SetViewOffset() is killing me here!

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 6th Feb 2013 05:21
Quote: "However as soon as I start with the screen moving using SetViewOffset() .. well now it only works near the center of the screen."

If your player never leaves the screen you could (theoretically) simply use the WorldToScreen commands. The angle should still be the same either way.

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 6th Feb 2013 05:23
@Hodgey: Yeah the player is always smack dab center of the screen. Its the world that moves around him.

I'll toy with that and see if it helps. Thanks.

PS: Anyone with other ideas don't be shy, I'll take them all!

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2013 12:05
If your player is always in the centre of the screen why not just use something like this:



this.mess = abs(sin(times#))
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 6th Feb 2013 16:07
@backslash: now that's a creative idea too! I think that'll work.

Was doing artwork last night so havent gotten to try either but I will definitely work on this tonight.

Thanks guys

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 6th Feb 2013 16:38
I think you'd need to add the view offset coordinates onto your mouse pointer coords - maybe best to split your code up a little, so work out the X and Y difference and store it as a variable, rather than doing the math directly in the Atanfull call. Firstly, because it'll make it more readable when you add in the view offset adjustment, but also because you'll need that if you plan to check the distance with SQRT as well, I tend to use DX# and DY# for X/Y distances as a rule just in case.

I got a fever, and the only prescription, is more memes.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Feb 2013 19:19 Edited at: 6th Feb 2013 19:28
i think the problem is that x from mouse is not x from sprite
if you use the viewoffset.

you can think ATanFull is the middle of a circle and x,y parameter are
one point of the circle around, the radius is unimportant because you get the same angle.

if you have two x,y coordinates you must subtract at both the x,y middle point you want. two coordinates because you will have the
delta / distance.
mx=x1 < your sprite is the middle point
x1=x1-mx
x2=x2-mx
dx=x2-x1

in theory you can trim it because x1-x1 =0,
its better if you understand your code and let the computer do
a little bit more.

try for the mouse a sprite and bring it at the same position as the pointer and use this both sprite x,y for calculating.
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 7th Feb 2013 05:49
Well an hour and 40 minutes of coding work later. I still have the exact same problem.

Believe I've tried all the suggestions several times over at this point

Ever feel frustrated?

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 7th Feb 2013 06:22
And here we are 40 minutes later this is working like so:



Now my movement code is a tiny wonky but the angles perfect.. think I am back to progress now.

I have no idea what I really did though haha!

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 7th Feb 2013 06:39
And movement bug fixed.

I spent 4 days on this code and I thought it would take 2-3 hours

What is persistence?

And thank you everyone for tipping in I really appreciate it, helped me think about it in different ways!

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2013 13:05
@erebusman
this function are usefull if you want handle ae sprite at middle,
i saw you use the left,top corner + width/2 height/2

setspriteoffset(spr,getspritewidth(spr)/2,getspriteheight(spr)/2)
setspritepositionbyoffset(spr,x,y)
getspritexbyoffset(spr)
getspriteybyoffset(spr)
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 7th Feb 2013 13:13
Markus, by default the offset of a sprite is in the centre of the sprite so you don't need to use setspriteoffset(spr,getspritewidth(spr)/2,getspriteheight(spr)/2) unless you have changed the offset previously. Just using getSpriteXbyOffset and getSpriteYbyOffset should do it otherwise. Just in case you thought (like I did for quite some time) that the offset was set to the top left corner by default


this.mess = abs(sin(times#))
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2013 13:47
@baxslash,thanks, good to know

Login to post a reply

Server time is: 2024-05-06 13:40:10
Your offset time is: 2024-05-06 13:40:10