
-----------------------------------
mobin12
Fri Dec 23, 2011 6:49 pm

Making a cricle follow the x and y of another
-----------------------------------
What is it you are trying to achieve?
trying to make the red circle to follow the x and y coordinates of the blue circle, no matter what (including time when the blue circle does not move)


What is the problem you are having?
Can't get the red circle to follow the blue circle around the screen


Describe what you have tried to solve this problem



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)




setscreen ("graphics: 800;500")
View.Set ("offscreenonly")
var x : int := 400 % Player x
var y : int := 250 % Player y
var x2 : int := 7 % Red enemy x
var y2 : int := 7 % Red enemy y
var chars : array char of boolean
loop
    drawfilloval (x, y, 10, 10, 55)
    drawfilloval (x2, y2, 7, 7, 41)
    Input.KeyDown (chars)
    if chars ('d') and x < 789 then
        x := x + 1
    end if
    if chars ('a') and x > 10 then
        x := x - 1
    end if
    if chars ('w') and y < 489 then
        y := y + 1
    end if
    if chars ('s') and y > 10 then
        y := y - 1
    end if
    View.Update
    delay (11)
    cls
end loop




Please specify what version of Turing you are using
Turing 4.1.1


-----------------------------------
Insectoid
Fri Dec 23, 2011 6:58 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
Looks to me like you haven't even tried yet. I see no code that uses x2 or y2.

-----------------------------------
mobin12
Sat Dec 24, 2011 8:36 am

Re: Making a cricle follow the x and y of another
-----------------------------------
i am clueless on how i should approach this problem

-----------------------------------
Insectoid
Sat Dec 24, 2011 9:13 am

RE:Making a cricle follow the x and y of another
-----------------------------------
There are two common ways to approach the problem here. 

The easy, ugly way: When do you want the red ball to go up? When do you want it to go down? Left? Right? The answer to that question is literally the code to do it.

The harder, prettier way: Use trig to calculate where the red ball should travel. I suggest you try the easy way first.

-----------------------------------
ThatGuyYouLove
Sat Dec 24, 2011 9:52 am

Re: Making a cricle follow the x and y of another
-----------------------------------
I think the easiest way to do this is to divide the screen into quarters around your enemy
i.e
Blueball(enemy) starts at 7, 7 or x2, y2 
then you compare it to yourself and your position, i.e x, y
therefore when you detect for where to move...
[code]if x2 > x then
x2 += 1
elsif x2 < x then
x2 -= 1
end if

if y2 > y then
y2 += 1
elsif y2 < y then
y2 -= 1
end if[/code]

So if they are past the enemy on either scale, the enemy moves towards them, and if they are even than they do nothing on that axis.

-----------------------------------
Insectoid
Sat Dec 24, 2011 10:40 am

RE:Making a cricle follow the x and y of another
-----------------------------------
You got that backwards bro. That code will make the red ball run away.

-----------------------------------
mobin12
Sat Dec 24, 2011 11:21 am

Re: Making a cricle follow the x and y of another
-----------------------------------
I WANT THE REDBALL TO MOVE TO THE POSITION OF THE BLUE, EVEN IF THE BLUE BALL IS NOT MOVING.

-----------------------------------
Insectoid
Sat Dec 24, 2011 12:28 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
You didn't answer the questions I asked.
When do you want the red ball to go up? When do you want it to go down? Left? Right?

-----------------------------------
Raknarg
Sat Dec 24, 2011 12:45 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
Here, I'll answer one of those for you.

1: When do you want the red ball to go up?
A: When the blue ball is above it.
Therefore: When the blue is above the red ball, then you want the red ball to go up. Fairly simple.

-----------------------------------
mobin12
Sat Dec 24, 2011 1:44 pm

Re: RE:Making a cricle follow the x and y of another
-----------------------------------
yes this is exactly my answer

Here, I'll answer one of those for you.

1: When do you want the red ball to go up?
A: When the blue ball is above it.
Therefore: When the blue is above the red ball, then you want the red ball to go up. Fairly simple.
and when the BLUE is on right of the RED, move the RED right
when the BLUE is on left of the RED, move the RED left
when the BLUE is on below of the RED, move the RED down
same with diagonals.

-----------------------------------
Insectoid
Sat Dec 24, 2011 2:06 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
I guess now the question is, how do you know when the red ball is above the blue ball?

And diagonals don't matter. They just happen, when you go left and right at the same time. If you do it right, you won't have to write a single line of code for diagonals.

-----------------------------------
mobin12
Sat Dec 24, 2011 4:00 pm

Re: RE:Making a cricle follow the x and y of another
-----------------------------------
I guess now the question is, how do you know when the red ball is above the blue ball?
RIGHT: When x > x2
LEFT: When x < x2
DOWN: When y < y2
UP: When y > y2

-----------------------------------
mobin12
Sat Dec 24, 2011 4:32 pm

Re: Making a cricle follow the x and y of another
-----------------------------------
This is my attempt
[code]
setscreen ("graphics: 800;500")
View.Set ("offscreenonly")
var x : int := 400 % Player x
var y : int := 250 % Player y
var x2 : int := 7 % Red enemy x
var y2 : int := 7 % Red enemy y
var chars : array char of boolean
loop
    drawfilloval (x, y, 10, 10, 55)
    drawfilloval (x2, y2, 7, 7, 41)
    Input.KeyDown (chars)
    if chars ('d') and x < 789 then
        x := x + 2
    end if
    if chars ('a') and x > 10 then
        x := x - 2
    end if
    if chars ('w') and y < 489 then
        y := y + 2
    end if
    if chars ('s') and y > 10 then
        y := y - 2
    end if
    % Attempt on enemy follow
    if x > x2 then
        x2 := x2 + 1
    end if
    if x < x2 then
        x2 := x2 - 1
    end if
    if y > y2 then
        y2 := y2 + 1
    end if
    if y < y2 then
        y2 := y2 - 1
    end if
    View.Update
    delay (15)
    cls
end loop[/code]

-----------------------------------
Insectoid
Sat Dec 24, 2011 5:45 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
That looks like it should work!

Protip:
[code]x2 := x2+1[/code]
can be written as [code]x2 += 1[/code]

It's the same thing. The 2nd is just nicer to read.

-----------------------------------
mobin12
Sat Dec 24, 2011 8:57 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
thanks also can you help me with collision dectection

-----------------------------------
Raknarg
Sun Dec 25, 2011 2:47 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
Sure. Have you tried to solve it yet?

-----------------------------------
ThatGuyYouLove
Mon Dec 26, 2011 12:14 pm

Re: RE:Making a cricle follow the x and y of another
-----------------------------------
You got that backwards bro. That code will make the red ball run away.

LOL thanks bro that's actually kinda funny.

For collision detection there are a few ways I can think of off the top of my head.



the simplest version of detection is just to detect for the center of one object to anywhere on another...
i.e


-----------------------------------
Velocity
Tue Dec 27, 2011 9:41 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
That seems about, right, ddi you get the answer to your question yet? cause i managed to get another solution.

-----------------------------------
Velocity
Tue Dec 27, 2011 9:44 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
@ insectoid that wasnt a protip (dont mean to judge your all holy and honorable proness) that was just standard procedure or another take of it, or less time consuming, or ... ugh this list can go on forever.

-----------------------------------
Aange10
Tue Dec 27, 2011 10:17 pm

Re: RE:Making a cricle follow the x and y of another
-----------------------------------
@ insectoid that wasnt a protip (dont mean to judge your all holy and honorable proness) that was just standard procedure or another take of it, or less time consuming, or ... ugh this list can go on forever.


Actually, it's an operator. Not a procedure. Secondly, if you were smart you'd realize he was being facetious. So,

@Velocity Cool story bro, you mad?

-----------------------------------
Insectoid
Tue Dec 27, 2011 10:47 pm

RE:Making a cricle follow the x and y of another
-----------------------------------
Thanks for having my back Aange10. *brofist*

Line shamelessly stolen from Tony. I think.

-----------------------------------
chipanpriest
Thu Dec 29, 2011 10:06 am

Re: Making a cricle follow the x and y of another
-----------------------------------
if x2 < x then 
    x2 += 1 
elsif x2 > x then 
    x2 -= 1 
end if 

if y2 < y then 
    y2 += 1 
elsif y2 > y then 
    y2 -= 1 
end if

this is literally all you need inside your code somewhere for the enemy to follow your guy

the collision code could be something like:
if x2 + x2radius >= x - xradius and y2 + y2radius = y - yradius
         and x2 - x2radius  12 then
        player (1) := player (1) - 2
    end if
    if chars ('d') and player (1) < 886 then
        player (1) := player (1) + 2
    end if
    if chars ('w') and player (2) < 514 then
        player (2) := player (2) + 2
    end if
    if chars ('s') and player (2) > 13 then
        player (2) := player (2) - 2
    end if

    % Attempt on enemy follow
    if player (1) > enemy (1) then
        enemy (1) := enemy (1) + 1
    end if
    if player (1) < enemy (1) then
        enemy (1) := enemy (1) - 1
    end if
    if player (2) > enemy (2) then
        enemy (2) := enemy (2) + 1
    end if
    if player (2) < enemy (2) then
        enemy (2) := enemy (2) - 1
    end if

    %Shockwave
    if chars (' ') and count > 0 and space = 0 then
        shockwaveValue (1) := player (1)
        shockwaveValue (2) := player (2)
        shockwaveValue (3) := 1
        shockwaveValue (4) := 1
        count := count - 1
        space := 1
    end if
    if not chars (' ') then
        space := 0
    end if

    %Display amount of shockwaves that are left
    if count = 3 then
        put "Shockwaves:"
        drawoval (133, 538, 7, 7, 39)
        drawoval (133, 538, 8, 8, 39)
        drawoval (154, 538, 7, 7, 39)
        drawoval (154, 538, 8, 8, 39)
        drawoval (175, 538, 7, 7, 39)
        drawoval (175, 538, 8, 8, 39)
    end if
    if count = 2 then
        put "Shockwaves:"
        drawoval (133, 538, 7, 7, 39)
        drawoval (133, 538, 8, 8, 39)
        drawoval (154, 538, 7, 7, 39)
        drawoval (154, 538, 8, 8, 39)
    end if
    if count = 1 then
        put "Shockwaves:"
        drawoval (133, 538, 7, 7, 39)
        drawoval (133, 538, 8, 8, 39)
    end if
    if count = 0 then
        put "Shockwaves:"
    end if

    %Firing the bullets
    mousewhere (mx, my, b)
    if b = 1 then
        drawline (player (1), player (2), mx, my, 43)
    end if

    %Enemy collision with shockwave
    if enemy (1) =0 and count