Making barriers with loops.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Csharp
Posts: 7
Joined: December 12th, 2012, 12:34 pm

Making barriers with loops.

Post by Csharp » January 15th, 2013, 2:32 pm

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"?

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Making barriers with loops.

Post by Musi » January 16th, 2013, 5:06 pm

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.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

FranklinBluth
Posts: 13
Joined: January 13th, 2013, 7:52 pm

Re: Making barriers with loops.

Post by FranklinBluth » January 16th, 2013, 6:17 pm

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! :D

EDIT:

After reading Musi's comment I realize that I basically just said everything he said, I just used less effeciency :lol:

Csharp
Posts: 7
Joined: December 12th, 2012, 12:34 pm

Re: Making barriers with loops.

Post by Csharp » January 16th, 2013, 9:52 pm

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.

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Making barriers with loops.

Post by Musi » January 16th, 2013, 10:49 pm

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.

Code: Select all

if( x >= box.x && x < box.x + box.width &&
    y >= box.y && y < box.y + box.height )
{
     // There is a collision.
} 
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.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Csharp
Posts: 7
Joined: December 12th, 2012, 12:34 pm

Re: Making barriers with loops.

Post by Csharp » January 16th, 2013, 11:38 pm

Thanks for the help. It does get quite confusing when thinking of the equations too much, please tell me this gets easier with time :D

Ill try this out tomorrow and let you know if I get it working

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Making barriers with loops.

Post by Musi » January 17th, 2013, 12:02 am

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.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Post Reply