Skip to content
Advertisement

How can i collide with a 45 degree slope?

I have spent the past 3 days trying to understand this but with every article I have read and every youtube video I have watched I have failed to comprehend the concept. I have given this my best go at listening and reading from what I have seen online, before asking here.

I just want one rectangle to go up one slope. I am not after a function for this. Each tutorial/article/video I watch explains how their function/class works. Some are using lists of classes and functions that call other functions inside them. It gets very abstract and it is to much for me right now.

I need someone to really baby this down for me it seems. No functions and no classes. I just want to move one rectangle up one 45 degree slope.

One youtube video explained that I need y = mx + b. But did not explain how to use this function. So if anyone out there has a formula for a basic 45 degree slope and can show me in my own code how to use it without functions or classes, i will be grateful.

After I understand it I can make my own functions and classes for implementing it. My attempt at this has been to make a rectangle behind my ramp image. Is this wrong? Should I be using a single line?

My attempt at code:

JavaScript

Advertisement

Answer

Calculate the height of the diagonal rectangle on the right edge of the moving rectangle (my_rect.right):

JavaScript

Compute the top of the the diagonal rectangle on the right edge of the moving rectangle:

JavaScript

The top of the the diagonal rectangle on the right edge of the moving rectangle is the bottom of the moving rectangle:

JavaScript

If the diagonal is in the opposite direction, find the height of the diagonal on the left edge (my_rect.left) instead of the right edge (my_rect.right).

The formula y = mx + b is hidden in this code:

x is my_rect.right - D_rect.left
m is -D_rect.height / D_rect.width
b is D_rect.bottom
y is my_rect.bottom


JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement