object.rotation question

Hi all,
In my app I have code which works out the angle between two points (“critter” and “food”) so that critter can move in a direct straight line towards the food.

local angle = math.atan2(food.y-critter.y ,food.x-critter.x)  
 critter.x = critter.x + (math.cos(angle) \* speed)  
 critter.y = critter.y + (math.sin(angle) \* speed)  

Now, as I understand it, the object.rotation command will rotate an object by x degrees not a specific angle. How can I get it so that the critter faces the food directly so he is always looking in the direction he is travelling?
I’ve tried various methods but they either throw up an error or just don’t do anything and I can’t figure out if I’m missing something really easy.

Cheers [import]uid: 7841 topic_id: 13498 reply_id: 313498[/import]

I assume you are using sprite sheets?

If you are, then you have to make it so that when going left for example, that it uses the coordinates of that sprite sheet that shows the character going that direction…
This shows how to attach to a dpad control scheme, shows sprite going up, down, left, right and uses the correct sprite on the sheet to do so.

Written by peach pellen

http://techority.com/2011/05/01/using-sprites-in-your-corona-iphone-application/

I am answering this based on the very little information you provided, you didn’t indicate if it was tilt, or dpad or press a button and character moves on it’s own or what?

Also, Choco Run does a “face direction that is traveled” type thing. It’s a game where the character runs in whatever direction and you can’t control it. You can only control jumping, so if you jump against and off a wall and he runs the other way, his sprite changes direction.

That code isn’t open though, so you would have to reach out to the author for help on how he did it :slight_smile:

anyway, maybe that helped maybe it didn’t. :slight_smile:

ng [import]uid: 61600 topic_id: 13498 reply_id: 49549[/import]

Take a look at my code on The Code Exchange and try that formula:

http://developer.anscamobile.com/code/rotate-object-finger [import]uid: 22392 topic_id: 13498 reply_id: 49557[/import]

Cheers for the prompt replies guys

@nicholasclayg
Yeah, sorry I didn’t explain it very well. It’s a top down game so the action is seen from above. The critter moves around the play area eating the food. He is totally autonomous and cannot be affected by the player at all. He simply moves in a straight line directly towards the food. When the food is eaten some more is spawned at a random location and he then moves off towards that which is why I would like to work out how to rotate him to the exact angle towards the food so that he is always facing it.

@ChunkyApps
I’ve had a quick look at your code and it seems I should be able to adapt it to suit my needs (without all of the touch stuff of course). When I get a bit more time to play I’ll have a go at implementing it.
If I’ve read it correctly, x1 & y1 would be the current co-ords for the critter, and x2 & y2 would be the co-ords of the food? [import]uid: 7841 topic_id: 13498 reply_id: 49571[/import]

No, I think it would be opposite that. Think of your finger as the food and the critter would rotate to face it. So x1 & y1 would be the current co-ords for the food, and x2 & y2 would be the co-ords of the critter.

Edit: Scratch that. I think you might be right. [import]uid: 22392 topic_id: 13498 reply_id: 49576[/import]

Poo. Now I’ve come to add the code I’ve suddenly noticed that I have no idea what x and y would be. If x1/y1 is the critter and x2/y2 is the food, then what is x/y that I have to take away when working out angle1/2? [import]uid: 7841 topic_id: 13498 reply_id: 49816[/import]

Does this work?

rotation = 180/math.pi * math.atan2(y2 - y1 ,x2 - x1) [import]uid: 22392 topic_id: 13498 reply_id: 49818[/import]