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.

DarkBASIC Discussion / Arcsine and arccosine functions: what's wrong?

Author
Message
Video Boy
21
Years of Service
User Offline
Joined: 21st Mar 2004
Location:
Posted: 10th Aug 2004 07:29 Edited at: 11th Aug 2004 01:19
I started to write a Tempest-like game with DBC v1.13 (well, now it has no interaction, it only displays the levels). The problem is that the levels (for which I use the Asin or Acos functions) are all screwed up! Or better, half of each level is screwed up, and which half depends from the function I use! Here I'll show you the snippet that actually displays the levels. Note that
1) the coordinates for each point in the level are stored in two 2-dimensional arrays: LEVELX(16,16) and LEVELY(16,16), and writing for example "LEVELX(1,4)" refers to the X coordinate of the 4th point in the 1st level
2) every object is a 10*0.1*10 box, which pivot point corresponds to its bottom-left vertex


In theory, this level should be a cylinder, but here's how it is distorted:
http://devilmaster.supereva.it/tempest1.jpg

If I change the code to use the Asin function, like in the following snippet...

...the opposite side is distorted, and this is the result:
http://devilmaster.supereva.it/tempest2.jpg
What am I doing wrong?
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Aug 2004 14:32
It'd help if you told us what you're trying to do.

"eureka" - Archimedes
Video Boy
21
Years of Service
User Offline
Joined: 21st Mar 2004
Location:
Posted: 11th Aug 2004 18:53 Edited at: 11th Aug 2004 20:58
1) First, I insert the X and Y coordinates into the LEVELX and LEVELY arrays (not shown here)

2) After that, I position each box (sized 10*0.1*10, which pivot point corresponds to its front-left vertex) to the corresponding X and Y coordinates


3) If this is the step that is unclear for you, here's a graph that shows the meaning of the variables:

I want the boxes to make an enclosed shape, so I have to find the cartesian distance and angle between the current point (I) and the next one (I+1). I calculate the distance with the Pythagorean theorem and scale the boxes accordingly:

Then I use the Asin or Acos functions.


The intention is to rotate each box so that it touches the next one (this is the kind of shape I want to obtain:

) but as you can see, it only works half of the time. Why?

P.S.
I modified the two screenshots I grabbed, to show the orientation of every segment. They should be oriented in a circle, but they are not!
http://devilmaster.supereva.it/tempest1.jpg
http://devilmaster.supereva.it/tempest2.jpg
Video Boy
21
Years of Service
User Offline
Joined: 21st Mar 2004
Location:
Posted: 11th Aug 2004 22:13
Quote: "I don't get this part..."

The Rotate Object instruction requires the angle to be between 0 and 360. The Asin function can generate negative angles, so I have to transform them into positive angles.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 12th Aug 2004 00:50
Though I have not yet personally tried the Asin or Acos functions in DBC at this point, your statement that the code "works only half the time" makes me wonder if you are getting the incorrect values returned for the quadrants your objects are in. Most of my previous experience with inverse functions returned only a + or - 90 degrees, rather than the full 360 which is required for proper placement. The resulting value of the angle returned, depending on the method used is to use the sign of the distance and the rises value to add or subtract the resulting angle from the 90 or -90 positions. (Or to mirror it in other words.)

Hope this helps.
S.

Any truly great code should be indisguishable from magic.
Video Boy
21
Years of Service
User Offline
Joined: 21st Mar 2004
Location:
Posted: 12th Aug 2004 04:32
Thank you SandraD, you centered the problem!
The problem was solved by modifying like this the second FOR cycle:

However, I have another question. How do I write the following line

DIST#=Sqrt(AC#^2+BC#^2)-2*Sqrt(AC#^2+BC#^2)

in a more decent way? If I write
DIST#=-Sqrt(AC#^2+BC#^2)
I get the error "Parameter mismatch. Array must have at least one subscript", and if I write
DIST#=Sqrt(AC#^2+BC#^2)
DIST#=-DIST#

I get "Syntax error. Unrecognized paramter"!
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Aug 2004 14:15
It's the "-" symbol throwing it off. Just put "dist# = 0-dist#". Though I'm not sure why you want a negative number.

"eureka" - Archimedes
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 12th Aug 2004 14:16 Edited at: 12th Aug 2004 14:17
Great! Glad to be of help...

As for the furmula, Dark Basic doesn't like the unary minus sign like that, the best solution is;

DIST#=0.0-Sqrt(AC#^2+BC#^2)

Good luck!
S.

Any truly great code should be indisguishable from magic.
BigDan256
21
Years of Service
User Offline
Joined: 29th Oct 2003
Location: NSW, Australia
Posted: 12th Aug 2004 18:36
How about the old Atanfull function:

ANGLE#=Wrapvalue(Atanfull(BC#,AC#))
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 13th Aug 2004 02:09
Yeah, atanfull() should work as well, unless the distance value was needed elsewhere in which case it would still need to be computed separately. But it "should" get rid of those if statements if used. I also note that the documentation in the manual claims that atan() returns angles in 360 degrees, but of course this is impossible...

Good luck!
S.

Any truly great code should be indisguishable from magic.
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Aug 2004 02:59
avatar thief!

I think I remember using the unary minus sign before. After running various codes I have with the newer patch, I've had to change a few programs. So its had to have worked before. Then again, it could be a bug because sometimes I'm allowed to use equations for the return type in a function, and other times it tells me a i cant.

endfunction x# - y#

"eureka" - Archimedes
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 13th Aug 2004 16:53 Edited at: 13th Aug 2004 16:54
I believe the only time the unary minus sign works presently is in the case of starting a formula with hard numbers as x=-1.3... and so on, due to some change made in the manner in which expressions are being parsed. Not exactly sure when this change took place in DBC, but it can be surprising when it occurrs.

And yes, there are times when functions are allowed to end with computations and others when they can not, again for reasons apparently unknown. The parsing routine must being having a good laugh on us, for it certainly is picky enough!

S.

Any truly great code should be indisguishable from magic.

Login to post a reply

Server time is: 2025-05-25 10:58:32
Your offset time is: 2025-05-25 10:58:32