What happens if you change the scale factor to 100.0, 150.0, ...?
# ==============================================================
# test/verify dot product
# ==============================================================
# from: Coding a Physics Engine from scratch!
# www.youtube.com/watch?v=nXrEX6j-Mws&t=122s
# --------------------------------------------------------------
# a dot product can tell you how far from a line a point
# is in the direction of the normal vector to the line
# (how far from the line)
#
# the normal vector is at right angles to the line
# --------------------------------------------------------------
# Suggestion: add a graphics display of the point in Cartesian
# coordinates (origin 0,0 center of the screen)
# ==============================================================
import math
import numpy as np
##import graphics as gr
VERBOSE = False
# --------------------------------------------------------------
# ---- title string of a given length
# --------------------------------------------------------------
def title_string(str1,length=64,char='-'):
str2 = ''
x = length - len(str1)
if x > 0: str2 = char*x
return str1 + str2
# --------------------------------------------------------------
# ---- display vector data
# --------------------------------------------------------------
def display_vector(v,x=None,y=None,ang=None,title=None):
if title is None: title = ''
print()
print(title_string(f'---- {title} vector ',50))
if x is not None: print(f'X = {x}')
if y is not None: print(f'Y = {y}')
if ang is not None: print(f'angle = {ang}')
print(f'vector = {v}')
##print(f'vectorX = {v[0]:.4f}')
##print(f'vectorY = {v[1]:.4f}')
# --------------------------------------------------------------
# ---- main
# --------------------------------------------------------------
# ---- normal vector (length 1.0)
normal_ang = 90.0
# ---- normal vector x,y coordinates
normal_x = math.cos(math.radians(normal_ang)) * 1.0
normal_y = math.sin(math.radians(normal_ang)) * 1.0
# ---- normal
normal = np.array([normal_x,normal_y])
display_vector(normal,title=f'normal ({normal_ang}) ')
# ---- point scaling factor
scale_factor = 1.0
# ---- points
for point_ang in [0,30,45,60,90,120,135,150,180]:
# ---- point x,y coordinates
point_x = math.cos(math.radians(point_ang)) * scale_factor
point_y = math.sin(math.radians(point_ang)) * scale_factor
# ---- point vector
point = np.array([point_x,point_y])
# ---- dot product
dot = np.dot(point,normal)
if VERBOSE:
display_vector(point,point_x,point_y,
title=f'point ({point_ang})')
else:
print(title_string(f'point ({point_ang}) ',50))
print(f'dot = {dot}')
## # ---- try reversing to parameter order
## dotx = np.dot(normal,point)
## print(f'dotx = {dotx}')
## # ---- try dot product equation
## x = normal[0]*point[0] + normal[1]*point[1]
## print(f'x = {x}')
Normal Vector to a Line
normal_vector_x = cos(90° + θ)
normal_vector_y = sin(90° + θ)
normal_vector_magnitude = 1.0
Definitions
| vector |
A quantity that has both magnitude (size or length) and direction.
It is used in physics and mathematics to represent quantities
like velocity, force, or displacement. |
| normal vector |
A vector perpendicular (orthogonal) to a surface, plane, line,
or a curve at a specific point, forming a 90° angle. |
Code From Video (Its Not Python)
# =======================================================================
# from: Coding a Physics Engine from scratch!
# https://www.youtube.com/watch?v=nXrEX6j-Mws&t=122s
# =======================================================================
class Circle
{
public var x:Float = 0;
public var y:Float = 0;
public var radius:Float = 0;
public var speedX:Float = 4.1;
public var speedX:Float = 4.1;
# ---- loss due to friction
public var friction:Float = -.01;
# ---- reduce the bounce speed over time
# ---- (loss of energy over time)
# ---- (high restitution ball keeps most of its energy)
# ---- (low restitution ball looses energy to sound and heat)
public var restitution:float = .9;
public function simulate()
{
if (y >= Main.groundY - radius)
{
if (speedY > 0)
{
speedY = -speedY;
y = Main.groundY - radius;
# ---- gravity added to vertical speed
speedY += Main.gravity;
}
}
for (line in Main.lines)
{
collideWithLine(line.position, line.normal);
}
x += speedX;
y += speedY;
}
function = collideWithLine(line:Point, normal:Point)
{
var distanceAwayFromLine = Mathz.dot(
x - line.x, y - line.y, normal.x, normal,y);
if (distanceAwayFromLine < radius)
{
var speedAlongNormal:Float = Mathz.dot(speedX, speedY,
normal.x, normal.y);
var speedAlongTangentl:Float = Mathz.dot(speedX, speedY,
normal.y, -normal.x);
if (speedAlongNormal <= 0)
{
speedX = -(speedAlongNormal * normal.x) * (restitution)
+ (speedAlongTangent * normal.y) * (1 - friction);
speedX = -(speedAlongNormal * normal.y) * (restitution)
+ (speedAlongTangent * -normal.x) * (1 - friction);
}
}
}
}