Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

Fantasy computer for making, playing and sharing tiny games. · By Nesbox

Having this simple line detection herre so I can refer to it in the future

A topic by minidavid created Jun 13, 2022 Views: 199 Replies: 5
Viewing posts 1 to 5
(2 edits)

```lua

------------PART 1-------------

---===DECLARING VARIABLES======

--1. We shall declare x  and y coordinates of two lines.

--i. Line 1 is a wall:

x1 = 9

y1 = 0

x2 = 9

y2 = 30

--ii.Line 2 is a ray:

x3 = 10

y3 = 10

x4 = 0

y4 = 10


-------------------------------

---------PART 2----------

---===TIC FUNCTION===---

--It's the main function

function TIC()

 cls()


--------PART 3 ------------

--===LETS DRAW THE TWO LINES==---

--1. We draw the wall

line(x1,y1,x2,y2,12)

--2. We draw the ray

line(x3,y3,x4,y4,12)



------------PART 4 -------------------

--===LINE COLLISION DETECTION==---

--1. In this section, we have to have some math, with three things: 

--   den, t and u.

--2. t and u are mathematical equations.

--3. If a collision happens,  0<t<1 and u>0.

--4. den is a denomitor declared as  a shortcut to find t and u

--    and also used to check whether itself is zero.

--   If it is zero, then it brings about an error.

-- If confused, don't worry, it'll be clear below. This is just an overview


--Slowly we shall do these in steps:


--1. We declare den, aka the denominator

den = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)


--2. We look for t and u. Therse are equations for getting the line detection.

 t = ((x1-x3)*(y3-y4)-(y1-y3)*(x3-x4))/den

 u = -((x1-x2)*(y1-y3)-(y1-y2)*(x1-x3))/den 


--3. As mentioned above, t and u are used for checking for a line collision

--   If 0<t<1 and u>0, then a collision between the ray line and the wall line has happened.

if (t>0 and t<1 and u>0) then

 print("true",100,100,12)

  return true

 else

   print("false",100,100,12)

  return nil

--we return nil as no collision has happened between the ray line and the wall line.

 end

--NOW, if the denominator is zero,

--it would be an error. Thus we return nil.

 if den == 0 then

  return nil

 end

end

```

(1 edit)

video I copied it from. I found that a lot of carts meant to be tutorials were too complicated and not broken down to the basic utilities for understanding how line collisions work

(9 edits)

Topdown jumping TIC80

title: game title

author: game developer

desc: short description

script: lua

t=0

x=96

y=24

a = 0

function rotate(x,y,a)

return x math.cos(a)-ymath.sin(a),

xmath.sin(a)+ymath.cos(a)

end

function TIC()

local x1,y1 = rotate(3,8,a)

local dotx= x1+x

local doty= y1+y

if btn(5) then a = a+0.4

x = x +2

a = a+0.1

x = math.floor(x)

end

cls(13)

spr(1+t%60//30*2,dotx,doty,14,3,0,0,2,2)

print("HELLO WORLD!",84,84)

print(x)

t=t+1

end

markdown being weird

(2 edits)

duster movement:

x = 100

y = 100

a = 0

function rotate(x,y,a)

return xmath.cos(a)-xmath.sin(a),

xmath.cos(a)+ymath.sin(a)

end

function TIC()

local r1,r2 = rotate(12,13,a)

local dotx = y+r1+a

local doty = y+r1

a = a + 0.1

cls(0)

pix(dotx2-100-r1-y,a3,12)

end

(1 edit)

weird alternative to math.sin((time()) but not really. Change speed by changing a = a + 0.1

x = 100

y = 100

a = 0

function rotate(x,y,a)

return xmath.cos(a)-xmath.sin(a),

xmath.sin(a)+ymath.cos(a)

end

function TIC()

local r1,r2 = rotate(12,13,a) local dotx = y+r1+a local doty = y+r1

a = a + 0.1

cls(0)

for i = 0,10 do

for j = 0,10 do

pix(i+dotx+j,j,i+j)

end

end

end