Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Ball move in orbit
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sakura




PostPosted: Sun May 30, 2004 8:03 pm   Post subject: Ball move in orbit

I was trying to move the ball in orbit..........

but i'm wondering if there's more efficient way than drawing it and erasing it everytime it moves

Thx a lot
Sponsor
Sponsor
Sponsor
sponsor
SuperGenius




PostPosted: Sun May 30, 2004 8:20 pm   Post subject: (No subject)

if you have oot 3.x you can use sprites but that version is pain in the ass because of no syntax colouring, some new commands are unsupported, and the fact that it just sucks.
AsianSensation




PostPosted: Sun May 30, 2004 8:47 pm   Post subject: (No subject)

well, if you know the equation of the ball's movement, is it a circle? or an ellipse? You can just model the math behind it.

like x^2 + y^2 = r^2 and x^2/a^2 + y^2/b^2 = 1

sub values into those equations will be the easiest way of modelling orbits.

for drawing and erasing, it's the only way. Though if you have 4.0.3 or above, View.Update is good.

and 4.0.5 gives View.UpdateArea, which is even better.
Flashkicks




PostPosted: Mon May 31, 2004 8:08 am   Post subject: (No subject)

Sensation; That is some very trippy stuff you got there..LOL.. i am going to play around with it and see if i can get anything to orbit. i have always wanted to make something orbit but never really understood the physics of how to actually PROGRAM it.. Maybe you could explain these numbers and whatnot.. . Would be nice. Either way- I shall go experiment Very Happy
~Flashkicks

<<EDIT>>
Okay- this is a few minutse later when i tried experimenting with those equations and I can NOT understand HOW they work.. i mean.. I dontthink first of all that i am typing NEARLY enuff code as to what may be needed- but then again even some of the most comlpex programs require little code.. This is what i have;
code:
%  "x^2 + y^2 = r^2"    and    "x^2/a^2 + y^2/b^2 = 1"
var x, y, r : int
x := 5
y := 5
r := 20

for i : 1 .. 20
r** = x** + y**
drawline (x, y, x + i, y + i, 12)
end for
Do you think you could help me with my coding or perhaps supply a sample of your own Working code??.. I know this thread wasnt my business or whatnot but I have been wondering about orbit for AGES now.. I dont really want to miss this chance to have someone explain to me how they work and the code for source and whatnot.. Anyhoo- Would be much appreciated....
~Flashkicks

btw- My Turing here at school doesnt recognize the "^" . Eg. x^2 .. hence; that is why i have "x**" <--bcuz that is how I am use to doing it. Does this create a problem?? Confused Crying or Very sad
guruguru




PostPosted: Mon May 31, 2004 3:24 pm   Post subject: (No subject)

You can only use "**" for anything to the power of someting else. YOu could just multiply the number by itself for squaring things.

Quote:
x^2 + y^2 = r^2

This is the equation of a circle (with center at origin).
r = radius
x = the x point
y = the y point

Changing the x (or y) and then changing the y (or x) to the appropriate value would make it orbit in a circle.

Quote:
x^2/a^2 + y^2/b^2 = 1

This is the equation of an ellipse (with center at origin).
x = the x point
y = the y point
a = distance out on the x axis from origin
b = distance out on the y aixs from origin

Changing a and b will change the stretch of the ellipse. If you made a extremely large, it would orbit in almost a horizontal line. If you mande b really large, it would orbit pretty much vertically.

To move the the shape, change the x and y to appropriate values. Same idea with the previouse equation.


Hope that helped :p.
Paul




PostPosted: Mon May 31, 2004 3:32 pm   Post subject: (No subject)

Im interested in this too, but never understood it completely, post a example code? maybe a code that draws a circle using drawdot
guruguru




PostPosted: Mon May 31, 2004 3:54 pm   Post subject: (No subject)

code:

var x, y, temp : real
var a, b, r : int

var centre : int := 100 % th


%%%%%% CIRCLE %%%%%%%

r := 50
x := 50

% Draw cicle by dots.
% Equation: (x - centre)**2 + (y-centre)**2 = r**2
% Since center is not 0, we have to incude the little minus thingy, just accept it :D

loop
    temp := r ** 2 - (x - centre) ** 2   % The equation above arranged above to isolate y squared at origin
    exit when temp < 0                   % You can't take a square root of a negative number (i :D)
    y := sqrt (temp) + centre% Find y... and move it to the appropriate center
    drawdot (round (x), round (y), black) % Draw the dot
    x += .1                              % Increase x by a random number
end loop



%%%%%% ELLIPSE %%%%%%%

centre := 250
a := 100 % width
b := 50 % height
x := centre - a

% Draw ellipse by dots.
% Equation: (x - centre)**2 / a**2 + (y-centre)**2 / b**2 = r**2
% (y-centre)**2 / b**2 := 1 - (x - centre)**2 / a**2
% (y-centre)**2 := b**2 - ((x - centre)**2 / a**2) * b**2
% (y-centre) := sqrt (b**2 - ((x - centre)**2 / a**2) * b **2)
%  Since center is not 0, we have to incude the little minus thingy, just accept it :D

loop
    temp := b ** 2 - ((x - centre) ** 2 / a ** 2) * (b ** 2) % The equation above arranged above to isolate y squared at origin
    exit when temp < 0                      % You can't take a square root of a negative number (i :D)
    y := sqrt (b ** 2 - ((x - centre) ** 2 / a ** 2) * (b ** 2)) + centre            % Find y... and move it to the appropriate center
    drawdot (round (x), round (y), black)   % Draw the dot
    x += .1                                 % Increase x by a random number
end loop


A quick example. I'll add the ellipse stuff in a second.

EDIT: OK everything is there. If you have questions ask away!
Paul




PostPosted: Mon May 31, 2004 4:48 pm   Post subject: (No subject)

well, about the drawing circle code, if I add
code:

r := 50
y := 50
loop
    temp := r ** 2 - (y - centre) ** 2   % The equation above arranged above to isolate y squared at origin
    exit when temp < 0                   % You can't take a square root of a negative number (i :D)
    x:= sqrt (temp) + centre% Find y... and move it to the appropriate center
    drawdot (round (x), round (y), black) % Draw the dot
    y += .1                              % Increase x by a random number
end loop

to it, it draws 3/4 of a circle, if I understood the concept, I would know how to figure out how to draw a full circle, but I don't... really know the concept, specifically the equations, what I know so far is:
the equation is the equation of a circle

code:

  temp := r ** 2 - (x - centre) ** 2
%and
y := sqrt (temp) + centre%

isolates and solves for y while x is changing and moves it to the correct spot relative to the center?, therefore the resulting x, y values because of the equation would form a circular shape.
but I don't really understand what center is for, except that it only works when the value of center is <x+r or y+r and is > the value of x or y.
guruguru




PostPosted: Mon May 31, 2004 5:15 pm   Post subject: (No subject)

Quote:
isolates and solves for y while x is changing and moves it to the correct spot relative to the center?, therefore the resulting x, y values because of the equation would form a circular shape.


Yep. Think of a circle. Lets say we start at the left side. Every second we move .1 to the right. Obviously, if we want to stay on the circles path, we would have to walk farther up. That is why we calculate Y. The reason I use a temp variable... as so...

code:

temp := r ** 2 - (x - centre) ** 2
exit when temp < 0                     
y := sqrt (temp) + centre


...is to make sure that we dont try to find the square root of a negative number. We will get a negative number when X is greater than or less than the two horizontal extremes of the circle. Since the circles path does not path those extremes, there is no possible way there can be any point on the circle out there. Hence we receive a negative number. (We could with complex and imaginary but not here :p.)

If you wanted to make a full circle, you would have to make a test to see if X has passed the extremities of the circle, and if it has, start reducing X instead of increasing it. We would also have to make y the negative of what the above gives us.

Quote:
but I don't really understand what center is for, except that it only works when the value of center is <x+r or y+r and is > the value of x or y.


Imagine the cartesian plane. The origin is (0, 0). Lets say radius is 50. And center is the origin. Our X in this example starts at 50. So when X = -50, Y = 0. When X = -25, Y = 25. When X = 0, Y = 50. etc... We don't have to manupulate the X and Y variables because the origin in (0, 0).

But now lets say that the center is (100, 100). Radius is still 50. Remember how when X = -50, y = 0? Well now, we add the X portion of the center to the X portion of our point. So X is now 50. And we do the same for Y so Y is now 100. Our new point on the left extreme of the circle is X = 50, Y = 100. Let's take... X = 25, Y = 25 with the origin as the center. Now lets convert that to our new center (100, 100). 25 + 100 = 125. 25 + 100 = 125. So new point is (125, 125). But now lets say our center is (100, 50). X = 25 + 100 = 125. Y = 50 + 50 = 100. Our new point on a circle with center (100, 50) is X = 125, Y = 100.

The equation of a circle in full form is this.
code:
(x - centre)**2 + (y-centre)**2 = r**2
When we make center the origin, the equation can be converted to
code:
x**2 + y**2 = r**2
since a number minus 0 is the number. But if we have a different center, things change as demonstarted above.

To isolate Y...

code:

(x - centre)**2 + (y-centre)**2 = r**2
(y-centre)**2 = r**2 - (x - centre)**2
(y - center) = sqrt (r**2 - (x - centre)**2)
y =  sqrt (r**2 - (x - centre)**2) + center


And there is the where the temp and y equations come from!
Paul




PostPosted: Mon May 31, 2004 5:47 pm   Post subject: (No subject)

Oh I see, I understand now, I think I'll mess around with it a bit now, thanx! Smile
Paul




PostPosted: Mon May 31, 2004 7:35 pm   Post subject: (No subject)

Oh Im sorry, I still don't fully understand enough to know how to draw a full circle Laughing I know about the extremeties, but when you subtract x, the y value goes back to from b4, and draws the top semi circle again, so heres what I did to construct a working orbital, it still seems icky to me cause I know I didn't do the right thing to make it, its an roundabout way. Laughing
code:

setscreen ('offscreenonly')
var x, y, temp : real
var a, b, r : int

var centre : int := 100 % th
var counter : int := 0

%%%%%% CIRCLE %%%%%%%

r := 50
x := 50

% Draw cicle by dots.
% Equation: (x - centre)**2 + (y-centre)**2 = r**2
% Since center is not 0, we have to incude the little minus thingy, just accept it :D

loop
    temp := r ** 2 - (x - centre) ** 2   % The equation above arranged above to isolate y squared at origin
    exit when temp < 0                   % You can't take a square root of a negative number (i :D)
    y := sqrt (temp) + centre % Find y... and move it to the appropriate center

    counter += 1
    % Increase x by a random number
    x += .1
end loop
var yv : array 1 .. counter of real
loop
x := 50
for z: 1..counter
 
    temp := r ** 2 - (x - centre) ** 2   % The equation above arranged above to isolate y squared at origin
    exit when temp < 0                   % You can't take a square root of a negative number (i :D)
    y := sqrt (temp) + centre % Find y... and move it to the appropriate center
    yv (z) := y-centre
    drawfilloval (round (x), round (y), 5, 5, black)
    View.UpdateArea (round(x)-6, round(y)-6, round(x)+6, round(y)+6)
    drawfilloval (round (x), round (y), 5, 5, white)
    % Increase x by a random number

   
    x += .1

end for
View.Update
cls
x :=149
% Draw the dot
for z : 1 .. counter
    temp := r ** 2 - (x - centre) ** 2 % The equation above arranged above to isolate y squared at origin
    exit when temp < 0                   % You can't take a square root of a negative number (i :D)
    y := centre-yv(z) % Find y... and move it to the appropriate center
   
    drawfilloval (round (x), round(y), 5, 5, black)
      View.UpdateArea (round(x)-6, round(y)-6, round(x)+6, round(y)+6)
    drawfilloval (round (x), round (y), 5, 5, white)
    % Increase x by a random number

    x -= .1
 
end for
View.Update
cls
end loop
guruguru




PostPosted: Mon May 31, 2004 8:14 pm   Post subject: (No subject)

The circle...

code:

var x, y, temp : real
var a, b, r : int
var yFactor : int := 1
var xNumber : real := .1
var centre : int := 200

r := 50
x := centre - r

loop
    if x >= centre + r then
        xNumber := -xNumber
        yFactor := -1
    end if
    temp := r ** 2 - (x - centre) ** 2
    y := yFactor * (sqrt (abs (temp)) + centre * yFactor)
    drawdot (round (x), round (y), black)
    x += xNumber
    exit when x <= centre - r
end loop


And the orbit Very Happy
code:

setscreen ("offscreenonly")

var x, y, temp : real
var a, b, r : int
var yFactor : int := 1
var xNumber : real := .2
var centre : int := 200

centre := 300
r := 50
x := centre - r

loop
    temp := r ** 2 - (x - centre) ** 2
    y := yFactor * (sqrt (abs (temp)) + centre * yFactor)
    cls
    drawfilloval (round (x), round (y), 5, 5, black)
    View.Update
    x += xNumber
   
    if x >= centre + r or x <= centre - r then
        xNumber := -xNumber
        yFactor := -yFactor
    end if
end loop


Note: Its choppy at the sides because it only sits a about two points near the sides. Changing the xNumber when ts at that point will fix it.
Flashkicks




PostPosted: Tue Jun 01, 2004 7:50 am   Post subject: (No subject)

Hey- i dont know about any of yous- but I found this thread to be very usefull..lol. Thanks very much for the examples laid out there- there are fine and dandy! I am going to still try to work with this and whatnot until i have it COMPLETELY figured out and I will become the MASTER of Orbits!! MwuHaHaHaaaaaaaaaaaa!!. And then- THE Wo0o0o0oRRRRRRRLD!!!!! *Pets pet Monkey*
DUN Dun Dunnnnnnnnnnnnnnnnnnnnnnnnnn...

Confused Rolling Eyes
~Flashkicks
guruguru




PostPosted: Tue Jun 01, 2004 4:23 pm   Post subject: (No subject)

Heh. Glad you found it useful... Maybe someone should make a tutorial on this.? Wink
Paul




PostPosted: Tue Jun 01, 2004 4:25 pm   Post subject: (No subject)

Yea, gather up all the info and make a grand tutorial on circular collision detection and the applications of the formula, I'd like to see how you could use this to go thru all the points on a circle for collision detection.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: