Proportional screen problems with registering and rounding touches....you'll see

local x = t.x
            local y = t.y
                
            local dis = calcDistance(x,y,self.ball.x, self.ball.y)
                
            while dis > self.range do
                if x > self.ball.x then x = x * .9999
                    else x = x * 1.0001 end
                if y > self.ball.y then y = y * .9999
                    else y = y * 1.0001 end
                    
                dis = calcDistance(x,y,self.ball.x,self.ball.y)
            end

So I have a character and he is surrounded by a force field type thing. If you click inside the force field, the character moves to the point that you tapped. If you tap outside, I want it to round it so that the position you clicked is proportional to a valid position.

Example: the ball is at 100, the range is 150. If you click the position vec2(0,400) it would scale the y down and the x down as until the values are within range…

calcDistance does what you’d assume it to do.

However I have 2 issues and 1 and maybe 1 “how would I completely redo this section of code”

So let’s go…

  1. When t.x or t.y are negative, program is caught in an infinite while (-1 * 1.001 just takes you further away from the intended value)
  2. When the screen cords become major…something like (10000000,1000000)… 1.0001 * whatever will give you a value far outside the range of the player. This is because the range is a constant but the position is not so my question: how do you scale it properly?
  3. How would I completely redo this? This question assumes this is an issue that someone else has faced before (maybe with their virtual joystick constrictions)…thoughts?

Anyway guys. I’m eager to hear your feedback and suggestions :slight_smile:

I believe you could accomplish this using something like: point:normalize() * math.min(allowed, point:dist(center))

point is where you touched, allowed is the radius of the circle you can touch in, center is the center of the circle you can touch in.

Can you show me exactly how and where you’d implement it? I’m a little lost aha

@JakAttak math.min*

@skyThecoder, I have absolutely no idea what’s up with that code…

Questions: what does normalize do? “Returns a Normalized the vector” isn’t the most obvious method definition…

@Invad3rZIM, well… i lied :stuck_out_tongue:

Here is how you can do it:

function setup()
    center = vec2( WIDTH / 2, HEIGHT / 2 )
    radius = WIDTH / 3
    touchPoint = vec2( WIDTH / 2, HEIGHT / 2)
end

function draw()
    background(0)

    fill(255, 0, 0)
    ellipse(center.x, center.y, radius * 2)

    fill(255, 255, 0)
    ellipse(touchPoint.x, touchPoint.y, 10)
end

function touched(t)
    local tp = vec2(t.x, t.y)

    local d = math.min(radius, tp:dist(center))
    local origin = vec2(0, 0)
    local angle = origin:angleBetween(tp - center)

    touchPoint = vec2(center.x + d * math.cos(angle), center.y + d * math.sin(angle))
end

Thank you kindly @jakattack

But what does normalize do…I’m curious

it returns a normalized vector :stuck_out_tongue:

it returns the vector specified but with length of 1 unit

Can you write out a simple example?

I successfully implemented that touch solution into my game :slight_smile:

glad it worked

How do I post vids on the forum? Just the hyperlink of it?

if it is youtube just paste the link

@JakAttak You know, trigonometry is much more expensive than normalization… There was nothing wrong with the original code.

@SkyTheCoder, did it work? I thought it might but I never actually tried it and from what Invader said I thought it wasn’t working.