Making barriers with loops.
Making barriers with loops.
Hi,
I've just finished lesson 12 of the beginner tutorials and was playing around with rotating lines.
I had a few attempts at making a line in the center of the screen which rotated, and when it collided with a box I had drawn (using for loops) it would push back the box in the direction that it was rotating.
However I got very confused of how I would make it function like this :
if (box == line)
{box = --}
I wasn't sure of how I could put the line equation (y=mx+b) from one function into an if statement in another function.
I hope I've made sense in explaining my problem any help/ideas would be greatly appreciated:)
p.s. even if there is an easy way to code "this entire line is a border, you cannot pass"?
I've just finished lesson 12 of the beginner tutorials and was playing around with rotating lines.
I had a few attempts at making a line in the center of the screen which rotated, and when it collided with a box I had drawn (using for loops) it would push back the box in the direction that it was rotating.
However I got very confused of how I would make it function like this :
if (box == line)
{box = --}
I wasn't sure of how I could put the line equation (y=mx+b) from one function into an if statement in another function.
I hope I've made sense in explaining my problem any help/ideas would be greatly appreciated:)
p.s. even if there is an easy way to code "this entire line is a border, you cannot pass"?
Re: Making barriers with loops.
I've never done it either but it might be a lot more complicated then it sounds.
I think you would have to test every pixel on the line for a collision, then you'd need find out how far away the box is from the center of the line because the further away from the center, the faster the line moves and more force would be applied to the box. You would also need to find the angle perpendicular to the line to find which way the box is suppose to move.
I think you would have to test every pixel on the line for a collision, then you'd need find out how far away the box is from the center of the line because the further away from the center, the faster the line moves and more force would be applied to the box. You would also need to find the angle perpendicular to the line to find which way the box is suppose to move.
Musi
There are 10 types of people that understand binary.
Those that do, and those that don't.
There are 10 types of people that understand binary.
Those that do, and those that don't.
-
- Posts: 13
- Joined: January 13th, 2013, 7:52 pm
Re: Making barriers with loops.
Ooh, this sounds like a really interesting problem...
How dynamic are you trying to make it? Will the box rotate? Is there going to be a full range of motion dependent upon how and where the box makes contact with the line?
You could run a loop that tests if any of the box pixels occupy the same location as a pixel in your line and then utilize some maths to generate a unique velocity and trajectory for the box dependent upon which pixel the box impacted in your line. Lever arms follow a simple 1:1 distance to force amplification ratio so nothing crazy would be needed.
The challenge would be formatting this equation to account for how the line pixels are defined ( Slope<1 defined by x, slope>1 defined by y )
Best of luck! looking forward to seeing the completed work if you choose to post it!
EDIT:
After reading Musi's comment I realize that I basically just said everything he said, I just used less effeciency
How dynamic are you trying to make it? Will the box rotate? Is there going to be a full range of motion dependent upon how and where the box makes contact with the line?
You could run a loop that tests if any of the box pixels occupy the same location as a pixel in your line and then utilize some maths to generate a unique velocity and trajectory for the box dependent upon which pixel the box impacted in your line. Lever arms follow a simple 1:1 distance to force amplification ratio so nothing crazy would be needed.
The challenge would be formatting this equation to account for how the line pixels are defined ( Slope<1 defined by x, slope>1 defined by y )
Best of luck! looking forward to seeing the completed work if you choose to post it!
EDIT:
After reading Musi's comment I realize that I basically just said everything he said, I just used less effeciency
Re: Making barriers with loops.
So to test for collision would I need to copy the entire line drawing function and place it inside of the if statement?
*edit
If so how would I phrase it so that it reads it as is equal to any of this equations results.
*edit
If so how would I phrase it so that it reads it as is equal to any of this equations results.
Re: Making barriers with loops.
You would basically use the draw line function but not inside an if statement. Once it's found the x and y of a pixel on the line, instead of calling PutPixel to draw it, test if its inside the box.
The hard part is handling the collision. I would think its much easier to collide with a circle than a square.
Edit: In fact, i just thought of a way to collide with a circle that doesn't involve testing every pixel. You could test if the distance perpendicular from the line to the center of the circle, is less than the radius of the circle. Then it just a matter of finding where the circle is to know which direction to move it in. Sound plausible.
Code: Select all
if( x >= box.x && x < box.x + box.width &&
y >= box.y && y < box.y + box.height )
{
// There is a collision.
}
Edit: In fact, i just thought of a way to collide with a circle that doesn't involve testing every pixel. You could test if the distance perpendicular from the line to the center of the circle, is less than the radius of the circle. Then it just a matter of finding where the circle is to know which direction to move it in. Sound plausible.
Musi
There are 10 types of people that understand binary.
Those that do, and those that don't.
There are 10 types of people that understand binary.
Those that do, and those that don't.
Re: Making barriers with loops.
Thanks for the help. It does get quite confusing when thinking of the equations too much, please tell me this gets easier with time
Ill try this out tomorrow and let you know if I get it working
Ill try this out tomorrow and let you know if I get it working
Re: Making barriers with loops.
No problem Csharp, hope you get it working.
Maybe just ignore my last idea, I'm not sure how to do that myself. Stick with testing every pixel, its easier on the brain.
I don't know if things get easier but the way i see it, once you learn something, you'll never have to learn it again. Thats what i tell myself anyway.
Maybe just ignore my last idea, I'm not sure how to do that myself. Stick with testing every pixel, its easier on the brain.
I don't know if things get easier but the way i see it, once you learn something, you'll never have to learn it again. Thats what i tell myself anyway.
Musi
There are 10 types of people that understand binary.
Those that do, and those that don't.
There are 10 types of people that understand binary.
Those that do, and those that don't.