Help needed with transition and rotation.

Hi all,
In my app I have a critter that moves across the screen to eat some food. When the food is eaten some more appears at a random point. What I would like is for the critter to rotate on the spot until it is facing the food and then start moving towards it.

This is what I have so far to do this...

1
2
3
4
if critter.mode=="scanning" then                                                                                                
local angle = math.atan2(food.y-critter.y ,food.x-critter.x)
transition.to(critter, { rotation = (angle*(180/math.pi)) , time = 2000, onComplete=moving()} )
end

Maybe create a table of all of the food that is available for placement.

Then, when the food appears, pass the name of the food and use those results in the transition.to statement.

You are also using some other logic I never saw before (scanning?). I would assume there are obstacles in the way or no? If there are you may need to implement something like A* or other pathfinding....

Ok so in this transition.to statement, the result will be the critter will immediately go towards the food.

transition.to(critter, { time=400, x = food.x, y =food.y } )

Be warned, I'm really stinky with tables. But I know on transition.to, you can use an object for something to animate towards it (as you are probably aware).

Also, this link has "point to a touch event". If you run the code at the link (run it in "debug") you will see the "line" (when in debug) on the square points to wherever you touch. With some magic massaging, I am sure you could alter it to work for you as well.

http://developer.anscamobile.com/code/making-object-point-touch-event

My code, shows a large box, and a small box. Drag the big box around and the small box will rotate a bit and chase after it. I just added this in, trying to help you out but I have to leave now to go run errands, so I think I was on the right track, no sure though. I'm not that good at math, or coding lol.

And now, for my code which may help, or may just make you mad :) either way, have at it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setScale( 60 )
physics.setGravity( 0, 0 )
physics.setPositionIterations(32)
physics.setDrawMode( "debug" )
 
local game = display.newGroup();
game.x = 0
game.y = 0
 
-- (Xlocation,Ylocation, Xsize, Ysize)
local square = display.newRect (0,0,50,50)
square:setFillColor( 0, 0, 255) 
physics.addBody( square,"dynamic",{ density = 0.1, friction=0.3, bounce=0.3,  } )
game:insert(square)
 
local box = display.newRect (50,50,150,150)
box:setFillColor( 255, 0, 0 ) 
physics.addBody( box, "dynamic", { friction=0.3, bounce=0.3} )
box.isBullet = true
game:insert( box)
 
local function dragsbox(event)
box.x = event.x
box.y = event.y
print "Dragging box"
end
 
--Function that will 
local function followbox(event)
  if event.phase == "began" then
  print "getting into position"
transition.to( square,  { time=500, delay=0, rotation = -event.x, -event.y,  } )
 elseif event.phase == "ended" then
transition.to(square, { delay=500, x = box.x, y =box.y, rotation = 80 } )
 
print "going towards box position"
end
end
 
--Spawn the square on screen so it doesn't spawn off screen :)
square.x= math.random(square.width/2,
display.contentWidth - square.width/2)
square.y= math.random(square.height/2,
display.contentHeight - square.height/2)
 
box:addEventListener ("touch", dragsbox)
box:addEventListener ("touch", followbox)

Hi Nicholas, thanks for your reply. Unfortunately it's not really what I was getting at.
When my critter eats the food , the new food appears and he then stands where he is, but rotates around until he sees the food then moves towards it.
The moving isn't a problem and works exactly how I want it to. The problem I have is with the rotation. In my code (as seen above) the "scanning" mode is just a setting for when the critter is looking for the food. I work out the angle between the critter and the food and then apply a transition.to with rotation to swing him round until he is facing it. This works, just not how I expected it to. It seems to be entirely random as to which direction the critter rotates.
Ideally what I would like is to be able to work out which direction (clockwise or anticlockwise) would be the quickest and then turn in that direction.

Hope that makes more sense :)

Is your critter just one physics body? If it is, then I can see how that random crap could appear.

Maybe what you could do is "weld" a (let's call it "refpoint" for the circle or box) small box or circle with an alpha of 0 and place the weld joint on the nose of the critter (so it's invisible) and then make distance calculations based off that? I would think that the critter needs a point of reference for which pointing is based. If you look at a square and you start in the center, it's random. But I think even changing the reference point (like setting it on a physics body) won't give you the results you want and instead maybe a weld joint might fix it.

So you would do something like:

calculate distance (calc distance) between refpoint and food

if refpoint distance > X
if refpoint distance > Y

then assign results to variable A

if refpoint distance < X
if refpoint distance < Y

then the results to variable B

If B is < A then take action and do rotate stuff

The last line, I am not sure how one would pass the results and then calculate and compare which direction to rotate.

Basically what I am getting at is the rotation direction would either be rotating left or right, or up or down based off the angle, and the results of the distance calculation by comparing the results.

Son of a B, that made my brain hurt. Now I want to know how to implement this!

Logically speaking I can see how to do it from just logic, but coding it would be.....interesting.

That's been my problem, I have crazy ideas for games then I get into this logic battle like you are and are saying to myself "WHY DO I TORTURE MYSELF WITH THIS!!!" :)

If I can cook something up I will, I find this fascinating.

I bet if that renvis technowand guy came in here, he would whip it out QUICK. That dude is crazy good :)

ng

ng

Hiya mate. Thanks for your reply again. Nope, there are no physics involved. I'm not using any for this game. What I have decided though, after a late night last night trying to figure this, is to leave it as it is for the time being and concentrate on geting the rest of the game worked on. I'll come back to it at a later date and hopefully see it from a fresh perspective.

Thanks for your assistance though, it is very much appreciated even though my problem didn't get solved :)

views:2319 update:2011/10/22 9:46:13
corona forums © 2003-2011