Making a cricle follow the x and y of another
| Author |
Message |
Raknarg

|
Posted: Sun Dec 25, 2011 2:47 pm Post subject: RE:Making a cricle follow the x and y of another |
|
|
| Sure. Have you tried to solve it yet? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
ThatGuyYouLove
|
Posted: Mon Dec 26, 2011 12:14 pm Post subject: Re: RE:Making a cricle follow the x and y of another |
|
|
Insectoid @ Sat Dec 24, 2011 10:40 am wrote: 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.
| Spoiler: |
the simplest version of detection is just to detect for the center of one object to anywhere on another...
i.e
| code: |
%Collision detection
%Paste this within your loop to check every time the balls move
if x2 <= (x +RadiusofBlue) and x2 >= (x - RadiusofBlue) then %broke these up for ease of reading, can be done in one if
if y2 <= (y + RadiusofBlue) and y2 >= (y - RadiusofBlue) then
Collision := true
end if
end if
|
there must be a more accurate version but the only one I can think of off the top of my head is...
| code: |
for radiuscount : -RadiusofRed .. RadiusofRed
if (x2 + radiuscount) <= (x +RadiusofBlue) and (x2 + radiuscount) >= (x - RadiusofBlue) then %broke these up for ease of reading, can be done in one if statement
if (y2 + radiuscount) <= (y + RadiusofBlue) and (y2 + radiuscount) >= (y - RadiusofBlue) then
Collision := true
end if
end if
end for
|
Checks for if anything on either axis touches.
| |
|
|
|
|
 |
Velocity

|
Posted: Tue Dec 27, 2011 9:41 pm Post subject: 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

|
Posted: Tue Dec 27, 2011 9:44 pm Post subject: 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

|
Posted: Tue Dec 27, 2011 10:17 pm Post subject: Re: RE:Making a cricle follow the x and y of another |
|
|
Velocity @ 27/12/2011, 8:44 pm wrote: @ 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

|
Posted: Tue Dec 27, 2011 10:47 pm Post subject: 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

|
Posted: Thu Dec 29, 2011 10:06 am Post subject: Re: Making a cricle follow the x and y of another |
|
|
| Turing: | 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:
| Turing: | if x2 + x2radius >= x - xradius and y2 + y2radius = y - yradius
and x2 - x2radius <= x + xradius and y2 - y2radius <= y + yradius then
exit
end if
|
|
|
|
|
|
 |
Dreadnought
|
Posted: Thu Dec 29, 2011 5:12 pm Post subject: Re: Making a cricle follow the x and y of another |
|
|
Most of what is posted above seems to be for rectangles (x+radius > x2-radius stuff). (perhaps with the exception of ThatGuyYouLove's second piece of code).
If we want to check for collision between two circles (which I think you are) there is an easy and "perfect" way to do it. Basically, check the distance between the centers (this is good since we already know where those are). Think of it like drawing a right angled triangle with the hypotenuse being the line between the centers and the other two sides being along the x and y axes. Since we are dealing with circles, we know that the maximum distance that the two centers can be from each other without the circles overlapping is the sum of the radii. Since the distance between the centers is the hypotenuse of our triangle, we just need to check if the hypotenuse is shorter than the sum of the radii. GOGO PYTHAGOREAN THEOREM! (or just use Math.Distance... same thing)
It could look something like this: | Turing: | var x1, y1, rad1 : int % First circle
var x2, y2, rad2 : int % Second circle
% Check for collision
if Math.Distance (x1, y1, x2, y2 ) < rad1 + rad2 then % This could be <= depending on preference
put "OMG A COLLISION!"
end if |
That's all you need to check collision between circles. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
chipanpriest

|
Posted: Fri Dec 30, 2011 10:38 am Post subject: Re: Making a cricle follow the x and y of another |
|
|
Dreadnought @ Thu Dec 29, 2011 5:12 pm wrote: GOGO PYTHAGOREAN THEOREM!
lol nice inspector gadget reference |
|
|
|
|
 |
mobin12
|
Posted: Mon Jan 02, 2012 11:09 am Post subject: Re: Making a cricle follow the x and y of another |
|
|
Alright guys if you are wondering what all this is for i will show you the other (big piece) of code for my final game:
| code: |
%Set the screen size
setscreen ("graphics:900;550")
%Anti-flicker command
View.Set ("offscreenonly")
var chars : array char of boolean
var enemy : array 1 .. 3 of int
enemy (1) := 7
enemy (2) := 7
enemy (3) := 1
var mx, my, b : int
var shockwaveValue, player : array 1 .. 4 of int
player (1) := 450
player (2) := 275
player (3) := 54
player (4) := 1
shockwaveValue (3) := 1
shockwaveValue (4) := 0
var space : int := 0
var count : int := 3
loop
%Draw the player
if player (4) = 1 then
drawfilloval (player (1), player (2), 10, 10, player (3))
drawfilloval (player (1), player (2), 8, 8, player (3) + 1)
end if
if enemy (3) = 1 then
drawfilloval (enemy (1), enemy (2), 7, 7, 41)
end if
Input.KeyDown (chars)
%Player controls
if chars ('a') and player (1) > 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) <= shockwaveValue (3) then
enemy (3) := 0
end if
if enemy (2) <= shockwaveValue (3) then
enemy (3) := 0
end if
%Enemy collision with bullet
if enemy (1) = mx and b = 1 then
enemy (3) := 0
end if
if enemy (2) = my and b = 1 then
enemy (3) := 0
end if
%Enemy collision with player
if enemy (1) + 7 <= (player (1) + 12) and enemy (1) + 7 >= (player (1) - 12) and enemy (2) + 7 <= (player (2) + 12) and enemy (2) + 7 >= (player (2) - 12) then
player (3) := 45
end if
%Draw the shockwaves
if shockwaveValue (4) = 1 then
drawoval (shockwaveValue (1), shockwaveValue (2), shockwaveValue (3), shockwaveValue (3), 41)
shockwaveValue (3) := shockwaveValue (3) + 7
drawoval (shockwaveValue (1), shockwaveValue (2), shockwaveValue (3), shockwaveValue (3), 40)
shockwaveValue (3) := shockwaveValue (3) + 7
drawoval (shockwaveValue (1), shockwaveValue (2), shockwaveValue (3), shockwaveValue (3), 39)
shockwaveValue (3) := shockwaveValue (3) + 7
if shockwaveValue (3) >= 1000 + 5 then
shockwaveValue (4) := 0
end if
end if
%Anti-flicker command
View.Update
%Slow the program down
delay (15)
%Clear the screen for the next loop
cls
end loop |
|
|
|
|
|
 |
mobin12
|
Posted: Mon Jan 02, 2012 11:11 am Post subject: RE:Making a cricle follow the x and y of another |
|
|
| all i need now is a health kind of thing for the player, a background, and maybe some gun sounds |
|
|
|
|
 |
evildaddy911
|
Posted: Mon Jan 02, 2012 4:18 pm Post subject: Re: Making a cricle follow the x and y of another |
|
|
health bars are very easy, i usually just write
right before the end loop
also, where you display the number of shockwaves left, you output "shockwaves" in every scenario, just write
| Turing: | if count>=0 and count<=3 then
put"Shockwaves:"
end if |
right before the draw commands
Also, you can save a few lines there by using "elsif" instead of
"end if
if", because the if statements are dealing with a single value being a specific number |
|
|
|
|
 |
mobin12
|
Posted: Tue Jan 03, 2012 12:09 pm Post subject: RE:Making a cricle follow the x and y of another |
|
|
| is 'health' another variable |
|
|
|
|
 |
Dreadnought
|
Posted: Tue Jan 03, 2012 2:27 pm Post subject: Re: Making a cricle follow the x and y of another |
|
|
| Yes, in his case health would be an integer variable representing both health and length (in pixels) of the health bar. |
|
|
|
|
 |
evildaddy911
|
Posted: Tue Jan 03, 2012 3:17 pm Post subject: Re: Making a cricle follow the x and y of another |
|
|
if your health variable is too large, the you can shrink the bar by using div,
div just means divide with out remainder ( mod ) or decimals ( / ), mostly used when you need an integer
if you want it to look a bit better, then use
max_health is another variable too.
backgrounds aren't too difficult, use Photoshop, fireworks, Google images, paint etc. to generate an image (make sure to save it as .jpg or .bmp), then,
which draws your picture with the bottom left corner at (0,0) |
|
|
|
|
 |
|
|