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 / How to draw a sprite as a line between two points (accurate)?

Author
Message
_Pauli_
AGK Developer
16
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 3rd May 2014 14:08 Edited at: 3rd May 2014 14:09
Hi,

I want to draw a sprite between two points (A, B). Both points are determined by pointer positions. I used this well known approach:



But I get some inaccurate results as you can see in this screenshot:



The circle sprite is centered at the A coordinates and the trianlge is at the B coordinates. As you can see the line angle is slightly off (but gets closer at angles that are a full multitude of 90 degrees). Also the length is not correct (at straight horizontal angles it is correct, and gets worse the more vertical it gets). I try to use floats where ever possible. All sprites are created from PNG images.
I feel like I'm doing some Math stuff wrong.

Can somebody help me out please?

Visit my blog: sebastianpauli.tumblr.com
Markus
Valued Member
22
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd May 2014 15:49
you should use as float or #
float at values without decimal places use .0

dX# = bX# - aX#
dY# = bY# - aY#
angle# = aTanFull(dX#, dY#)

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
_Pauli_
AGK Developer
16
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 3rd May 2014 15:52
Yeah I know, I have all variables declared as float like so:



That's how I do it always, because it's more like C style and I don't have to include special characters in my variable names.

Visit my blog: sebastianpauli.tumblr.com
Hockeykid
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 3rd May 2014 18:51 Edited at: 3rd May 2014 18:52
It might have something to do with your sprite offset. Could you possibly post all of the code?



Sean

Markus
Valued Member
22
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd May 2014 18:53
hmm, i can not help. have save effect.
seems this ia a bug in aTanFull or setSpriteAngle.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Hockeykid
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 3rd May 2014 19:00
I just setup some test code of my own, have you declare "angle" as a float?

This code works for me (I have it following my mouse), but when not declaring "angle" as a float I see the same issue you're having:





Sean

lilpissywilly
AGK Developer
15
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 3rd May 2014 19:27
Are you perhaps using SetDisplayAspect() instead of SetVirtualResolution()?

My hovercraft is full of eels
_Pauli_
AGK Developer
16
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 3rd May 2014 20:03
Quote: "This code works for me"


Hockeykid's code works for me, too! Even if I do this:



It then works correctly, the sprite follows the pointer, but as expected in integer intervals (it skips angular intervals that are smaller than 1).

Quote: "Are you perhaps using SetDisplayAspect() instead of SetVirtualResolution()?"


Ok, this seem to be the root of the problem! This line breaks Hockeykid's sample code if I put it at the top:



Using setVirtualResolution() instead leads to correct results! Thanks a lot for the advice!

Do you have any explanation for this behavior?
Are there some internal inaccuracies happening between screen and world coordinate conversion?

Visit my blog: sebastianpauli.tumblr.com
Hockeykid
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 3rd May 2014 20:19 Edited at: 3rd May 2014 20:20
Quote: "Do you have any explanation for this behavior?"


Using SetDisplayAspect() will position objects based on screen percentage, where 0,0 is the top left, 0,100 is top right, 100,0 is bottom left, and 100,100 is bottom right. You might be experiencing this issue using the percentage system due to round-off error.


Sean

lilpissywilly
AGK Developer
15
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 3rd May 2014 21:31
I think you can solve it by multiplying the X (or is it Y?) factors by the aspect ratio. Maybe that explains why it doesn't work.

I don't think it's rounding errors, you can use decimals with the percentage system.

My hovercraft is full of eels

Login to post a reply

Server time is: 2026-07-27 22:40:53
Your offset time is: 2026-07-27 22:40:53