Collision problem in my code [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

Collision problem in my code [SOLVED]

Post by test » Tue Apr 23, 2019 3:02 pm

What is the problem? It is not accurate.

Code: Select all

local w
local h

local p = {}

p[1] = {}
p[2] = {}

p[1].touch = {}
p[2].touch = {}

local iceSpikeSlot
local airSwipeSlot

local projectiles = {}

local iceSpike
local airSwipe

function love.load()
	w, h = love.graphics.getDimensions()

	p[1].x = w / 4
	p[2].x = 3 * w / 4

	for i = 1, 2 do
		p[i].img = love.graphics.newImage('p.png')
		p[i].y = h / 2
		p[i].w = p[i].img:getWidth()
		p[i].h = p[i].img:getHeight()
		p[i].element = love.math.random(1, 2)
		p[i].touch.x = p[i].x
		p[i].touch.y = p[i].y
		p[i].defaultSpeed = 750
		p[i].speed = p[i].defaultSpeed
		p[i].cd1 = 0
		p[i].cd2 = 0
		p[i].cd3 = 0
		p[i].cd4 = 0
		p[i].defaultSpeedTimer = 0
	end

	iceSpikeSlot = love.graphics.newImage('iceSpikeSlot.png')
	airSwipeSlot = love.graphics.newImage('airSwipeSlot.png')

	iceSpike = love.graphics.newImage('iceSpike.png')
	airSwipe = love.graphics.newImage('airSwipe.png')
end

function love.update(dt)
	local touches = love.touch.getTouches()

	for i, id in ipairs(touches) do
		local x, y = love.touch.getPosition(id)

		if x < w / 2 then
			if x > 150 then
				p[1].touch.x = x
				p[1].touch.y = y
			else
				if y < 150 then
					if p[1].cd1 <= 0 then
						if p[1].element == 1 then
							local dir = math.atan2(p[1].touch.y - p[1].y, p[1].touch.x - p[1].x)
							table.insert(projectiles, {type = 1, owner = 1, img = iceSpike, x = p[1].x, y = p[1].y, w = iceSpike:getWidth(), h = iceSpike:getHeight(), dir = dir, speed = 1000})
							p[1].cd1 = 2
						elseif p[1].element == 2 then
							local dir = math.atan2(p[1].touch.y - p[1].y, p[1].touch.x - p[1].x)
							table.insert(projectiles, {type = 2, owner = 1, img = airSwipe, x = p[1].x, y = p[1].y, w = airSwipe:getWidth(), h = airSwipe:getHeight(), dir = dir, speed = 750})
							p[1].cd1 = 1
						end
					end
				elseif y < 300 then
					if p[1].element == 1 then
					elseif p[1].element == 2 then
					end
				elseif y < 450 then
					if p[1].cd3 <= 0 then
						if p[1].element == 1 then
						elseif p[1].element == 2 then
							p[1].x = love.math.random(0, w / 2)
							p[1].y = love.math.random(0, h)
							p[1].touch.x = p[1].x
							p[1].touch.y = p[1].y
							p[1].cd3 = 10
						end
					end
				elseif y < 600 then
					if p[1].element == 1 then
					elseif p[1].element == 2 then
					end
				end
			end
		else
			if x < w - 150 then
				p[2].touch.x = x
				p[2].touch.y = y
			else
				if y > h - 150 then
					if p[2].cd1 <= 0 then
						if p[2].element == 1 then
							local dir = math.atan2(p[2].touch.y - p[2].y, p[2].touch.x - p[2].x)
							table.insert(projectiles, {type = 1, owner = 2, img = iceSpike, x = p[2].x, y = p[2].y, w = iceSpike:getWidth(), h = iceSpike:getHeight(), dir = dir, speed = 1000})
							p[2].cd1 = 2
						elseif p[2].element == 2 then
							local dir = math.atan2(p[2].touch.y - p[2].y, p[2].touch.x - p[2].x)
							table.insert(projectiles, {type = 2, owner = 2, img = airSwipe, x = p[2].x, y = p[2].y, w = airSwipe:getWidth(), h = airSwipe:getHeight(), dir = dir, speed = 750})
							p[2].cd1 = 1
						end
					end
				elseif y > h - 300 then
					if p[2].element == 1 then
					elseif p[2].element == 2 then
					end
				elseif y > h - 450 then
					if p[2].cd3 <= 0 then
						if p[2].element == 1 then
						elseif p[2].element == 2 then
							p[2].x = love.math.random(w / 2, w)
							p[2].y = love.math.random(0, h)
							p[2].touch.x = p[2].x
							p[2].touch.y = p[2].y
							p[2].cd3 = 10
						end
					end
				elseif y > h - 600 then
					if p[2].element == 1 then
					elseif p[2].element == 2 then
					end
				end
			end
		end
	end

	for i = 1, 2 do
		local distance = math.dist(p[i].x, p[i].y, p[i].touch.x, p[i].touch.y)
		local stepSize = p[i].speed * dt

		if distance > stepSize then
			p[i].x = p[i].x + (p[i].touch.x - p[i].x) / distance * stepSize
			p[i].y = p[i].y + (p[i].touch.y - p[i].y) / distance * stepSize
		end
	end

	for i, v in ipairs(projectiles) do
		v.x = v.x + math.cos(v.dir) * v.speed * dt
		v.y = v.y + math.sin(v.dir) * v.speed * dt

		if v.x < v.w / 2 or v.x > w - v.w / 2 then
			table.remove(projectiles, i)
		end

		if CheckCollision(v.x, v.y, v.w, v.h, p[1].x, p[1].y, p[1].w, p[1].h) then
			if v.owner == 2 then
				table.remove(projectiles, i)
				if v.type == 1 then
					p[1].speed =  p[1].speed / 2
					p[1].defaultSpeedTimer = 2
				else
				end
			end
		end

		if CheckCollision(v.x, v.y, v.w, v.h, p[2].x, p[2].y, p[2].w, p[2].h) then
			if v.owner == 1 then
				table.remove(projectiles, i)
				if v.type == 1 then
					p[2].speed =  p[2].speed / 2
					p[2].defaultSpeedTimer = 2
				else
				end
			end
		end
	end

	for i = 1, 2 do
		if p[i].cd1 > 0 then
			p[i].cd1 = p[i].cd1 - dt
		end
		if p[i].cd2 > 0 then
			p[i].cd2 = p[i].cd2 - dt
		end
		if p[i].cd3 > 0 then
			p[i].cd3 = p[i].cd3 - dt
		end
		if p[i].cd4 > 0 then
			p[i].cd4 = p[i].cd4 - dt
		end
	end

	for i = 1, 2 do
		if p[i].defaultSpeedTimer > 0 then
			p[i].defaultSpeedTimer = p[i].defaultSpeedTimer - dt
		elseif p[i].defaultSpeedTimer < 0 then
			p[i].speed = p[i].defaultSpeed
		end
	end
end

function love.draw()
	love.graphics.line(w / 2, 0, w / 2, h)

	love.graphics.line(150, 0, 150, h)
	love.graphics.line(w - 150, 0, w - 150, h - 150)

	if p[1].element == 1 then
		love.graphics.draw(iceSpikeSlot, 0, 0)
	elseif p[1].element == 2 then
		love.graphics.draw(airSwipeSlot, 0, 0)
	end

	if p[2].element == 1 then
		love.graphics.draw(iceSpikeSlot, w - 150, h - 150)
	elseif p[2].element == 2 then
		love.graphics.draw(airSwipeSlot, w - 150, h - 150)
	end

	for i = 1, 2 do
		love.graphics.draw(p[i].img, p[i].x - p[i].w / 2, p[i].y - p[i].h / 2)
	end

	for i, v in ipairs(projectiles) do
		love.graphics.draw(v.img, v.x, v.y, v.dir, 1, 1, v.w / 2, v.h / 2)
	end
end

function math.dist(x1, y1, x2, y2) return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5 end

function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2) return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1 end
Attachments
main.lua
(6.46 KiB) Downloaded 37 times
game.love
(3.95 KiB) Downloaded 63 times
Last edited by test on Fri May 17, 2019 9:59 am, edited 3 times in total.

dusoft
Party member
Posts: 108
Joined: Fri Nov 08, 2013 12:07 am

Re: Collision problem in my code

Post by dusoft » Sat Apr 27, 2019 9:25 am

Please, stop posting *just* your code and asking questions.

Ask concise questions with exact error Love/Lua is giving you and a sample of your code. Otherwise you are wasting time of other people.

test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

Re: Collision problem in my code

Post by test » Sat Apr 27, 2019 2:07 pm

im so sorry. how can i remove my thread

dusoft
Party member
Posts: 108
Joined: Fri Nov 08, 2013 12:07 am

Re: Collision problem in my code

Post by dusoft » Sat Apr 27, 2019 4:47 pm

Well, how about instead of removing thread, you ask what is not accurate and post the error (or pointing difference to a result you are aiming for)? Which variable, what value etc.

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Collision problem in my code

Post by raidho36 » Sat Apr 27, 2019 9:14 pm

Being able to formulate your problem is 50% of the solution.

User avatar
Darlex
Party member
Posts: 126
Joined: Sun Sep 24, 2017 10:02 am
Location: %_
Contact:

Re: Collision problem in my code

Post by Darlex » Fri May 03, 2019 6:58 pm

test wrote:
Sat Apr 27, 2019 2:07 pm
im so sorry. how can i remove my thread
Dont delete it! We are here to help you. But you need to give us more information.
To edit a post; press the pen button at the superior right corner.

But if you want to delete it just press the "X" one
*funny coding joke*

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests