Analog stick question Help!

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Analog stick question Help!

Post by my psprecious »

I move around a png with the analog,
I use a colorpixel detection for my collision,
I'm able to get it to stop when it hit the color but then i can't move , what must I do to get the analog stick to continu moving the png without passing the red zone (like an obstacle)
while true do

screen:clear()
analogS() --- analog function that I have ealier in my code
if bgC:pixel(x1, y1) == red then
move = false
else
move = true
end
if move == true then x1 = x0 ; y1 = y0
end
this is part of the code, where the pixel detection and stop happens
Last edited by my psprecious on Mon Feb 20, 2006 7:49 am, edited 3 times in total.
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

You are stopping once you are ON the red pixel. What you probably want to do is stop before you get there. Try this:

Code: Select all

while true do 

screen:clear() 
analogS() --- analog function that I have earlier in my code 
if bgC:pixel(x0, y0) == red then 
move = false 
else 
move = true 
end 
if move == true then x1 = x0 ; y1 = y0 
end  
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

Thanx it's not that yet but it seems to be closer to my goal since I don't get stop, now it kind breaks and jump over but I think I might manage

except if got more great ideas like this one cause it really help the fact it doesn't stop totaly really helps me out now I feel I'm going somewhere, well the dot is hehe

big thx
fattymc03
Posts: 18
Joined: Sat Feb 11, 2006 12:22 am

Post by fattymc03 »

I'm not sure exactly where your problem is. Can we see your analogS() function. Also, this is a more efficient way to code your loop. You don't need that variable.

Code: Select all

 
while true do

screen:clear()
analogS() --- analog function that I have earlier in my code
if bgC:pixel(x0, y0) ~= red then
x1 = x0 ; y1 = y0
end  
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

Thanx mate here's the analog function
function analogS()

pad = Controls.read()

dx = pad:analogX()
if math.abs(dx) > 32 then
x0 = x0 - dx / 64
end
dy = pad:analogY()
if math.abs(dy) > 32 then
y0 = y0 - dy / 64
end

What I'm trying to is to be able to go around the red pixel zone but never be able to go in or on it
fattymc03
Posts: 18
Joined: Sat Feb 11, 2006 12:22 am

Post by fattymc03 »

So, it stops you before entering the red area correct? If yes, the detection is working fine, its somewhere else in your code that is not enabling you to move. And you say you can't move at all once it reaches this red area. Like you can't move backwards. Does movement work perfect without reaching the red area? Is the x0/y0 your current position and x1/y1 the next position. It's hard to tell, at least to me, from just this code.
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

red = Color.new(255, 0, 0)

dot = Image.load("DATA/dot.png")
bgC = Image.load("DATA/bg col2.png")

x1 = 0 ; x0 = 0 ; y1 = 0 ; y0 = 0



------------------------------------------
---------Function Analog Stick------------
------------------------------------------

function analogS()

pad = Controls.read()

dx = pad:analogX()
if math.abs(dx) > 32 then
x0 = x0 + dx / 64

end
dy = pad:analogY()
if math.abs(dy) > 32 then
y0 = y0 + dy / 64

end
end


board = Image.createEmpty(480, 272)
while true do

screen:clear()
analogS() --- analog function
if bgC:pixel(x0, y0) == red then
move = false
--and
--analogS()
else
move = true
end
if move == true then x1 = x0 ; y1 = y0
end
board:clear()
board:blit(0, 0, bgC, 0, 0, bgC:width(), bgC:height(), false)
board:blit(x1, y1, dot, true)
screen:blit(0, 0, board)
screen.waitVblankStart()
screen.flip()

if pad:start() then break end

end
here's the entire code, now with x0, y0 in the pixel detection, it doesn't totally stop, but it's not what I'm trying to do I want it to feel the edge of the red zone, so been able to go around not teleport on the otherside like it does now

Big thanx for your help
fattymc03
Posts: 18
Joined: Sat Feb 11, 2006 12:22 am

Post by fattymc03 »

The problem is that it keep moves even if you have hit the red spot once. Suppose you run into it by pressing left. It won't actually display the new spot of your dot but it still moves the x0 and y0, so eventually it is past the red area and thats why it "jumps" over it. If you want it to go around it, you need to kinda "stop" the updating of the x0 and y0 positions. I might suggest making boolean variables like moveLeft, moveRight, etc that will tell you if you can move in a certain direction. Then in the analog stick function, only update a certain x or y if you can move in that direction. Like:

if analog pressed left and moveLeft = true then
updateX
...
etc

in your detection code, update the boolean variables based on direction pressed and if it hit a red area, etc

Hope this helps.
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

Ok what it was doing I pretty much understood, it's the analog part that was bugging me I think I was under the illusion that the analog couldn't do that

But now that you tell me I can control it like the d pad, this shouldn't be to hard

big thanx man someone sugested to detect pixel around I think that's when I got confuse
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

fattymc03 wrote:The problem is that it keep moves even if you have hit the red spot once. Suppose you run into it by pressing left. It won't actually display the new spot of your dot but it still moves the x0 and y0, so eventually it is past the red area and thats why it "jumps" over it. If you want it to go around it, you need to kinda "stop" the updating of the x0 and y0 positions.
This is kind of what I was suggesting with the fix I posted earlier. You need to get clear on what the different variables are being used for. If you decide that (x1, y1) is the current location and (x0, y0) is the location you are possibly going to move to, then you only need to fix your analogS() function to calculate the new position (x0, y0) based on the current position (x1, y1) plus the motion vector (dx/64, dy/64):

Code: Select all

function analogS()
   pad = Controls.read()

   dx = pad:analogX()
   if math.abs(dx) > 32 then
      x0 = x1 + dx / 64
   end
   dy = pad:analogY()
   if math.abs(dy) > 32 then
      y0 = y1 + dy / 64
   end
end
my psprecious
Posts: 24
Joined: Fri Feb 17, 2006 5:08 am

Post by my psprecious »

wow that makes so much sense

but what I get is weird, what happens is the get stock on the red pixel and to move away I need to go the opposite way, if I go up and down even if there is no red pixel it stays there. (would be great for a spider man game)
Post Reply