Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 06-24-2010, 10:20 AM   #1 (permalink)
Registered Member
 
ataranlen's Avatar
 
Join Date: Jun 2009
Location: Flower Mound, Texas
Age: 22
Posts: 67
ataranlen is on a distinguished road
Default Problem turning an object

I'm working on an app, this is my first time using Cocos2d, but I don't think thats my problem.

I have a piece of code here that is supposed to turn an object Clockwise when you press and hold a button, also changing its direction to go at the same angle.
Code:
id action = [RotateBy actionWithDuration:0.01 angle:1];
	id seq = [Sequence actions:action, nil];
	[bg2 runAction:seq];
	directionIdx ++;
	directionId2 ++;
	if (directionId2 > 90)
		directionId2 -= 90;
	if (directionIdx > 360)
		directionIdx -= 360;
	
	if (directionIdx <= 360) {	
		if (directionIdx <= 270) {
			if (directionIdx <= 180) {
				if (directionIdx <= 90){
					newx = 0+(1/90* directionId2);
					newy = 1-(1/90* directionId2);
				} else {
				newx = 1-(1/90* directionId2);
				newy = 0-(1/90* directionId2);
				}
			} else {				
			newx = 0-(1/90* directionId2);
			newy = -1+(1/90* directionId2);
			}
		} else {
		newx = -1+(1/90* directionId2);
		newy = 0+(1/90* directionId2);
		}
	}
This code is to move the object in a straight line. The Timer for this function is paused while the object is turning.
Code:
-(void)MoveObject {
	oldx = newx;
	oldy = newy;
	CGPoint currentPos = [bg2 position];
	CGPoint diff = CGPointMake(oldx, oldy);
	[bg2 setPosition: ccpAdd(currentPos, diff)];
	
}
Here is what I have thought of, but not tried:
Setting newx and newy manually for each angle in the 360 degree circle
__________________
Nathan Stoltenberg
Lead Programmer - Fractal Fox Studios
Mobile Applications Consultant - The Principal Consulting

Apps I've coded:
Greeble, Cat Juggling, and Garden 2 Go, Ed Meets The Dentist

Bitcoin Donation Address:
1CJd8W6JiGKEgKHYp4PvWXWsnemHd8uqr7
ataranlen is offline   Reply With Quote
Old 06-25-2010, 10:38 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

This may be your problem:
Code:
newx = 1-(1/90* directionId2);
newy = 0-(1/90* directionId2);
1/90 ALWAYS is zero because you are doing an integer division. What you need is this:
Code:
newx = 1-(1.0/90* directionId2);
newy = 0-(1.0/90* directionId2);
smithdale87 is offline   Reply With Quote
Old 06-25-2010, 11:22 AM   #3 (permalink)
Registered Member
 
ataranlen's Avatar
 
Join Date: Jun 2009
Location: Flower Mound, Texas
Age: 22
Posts: 67
ataranlen is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
This may be your problem:
Code:
newx = 1-(1/90* directionId2);
newy = 0-(1/90* directionId2);
1/90 ALWAYS is zero because you are doing an integer division. What you need is this:
Code:
newx = 1-(1.0/90* directionId2);
newy = 0-(1.0/90* directionId2);
Wow, how did i miss that one? Thanks for pointing that out. I actually used a workaround and did it programatically for turning by 5 degrees. Would have been nice to figure this out before i went and typed out the code 72 times for each 5 degree turn.
__________________
Nathan Stoltenberg
Lead Programmer - Fractal Fox Studios
Mobile Applications Consultant - The Principal Consulting

Apps I've coded:
Greeble, Cat Juggling, and Garden 2 Go, Ed Meets The Dentist

Bitcoin Donation Address:
1CJd8W6JiGKEgKHYp4PvWXWsnemHd8uqr7
ataranlen is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 370
9 members and 361 guests
.Snipe, AragornSG, baja_yu, ChrisYates, davejas69, guusleijsten, hussain1982, Kryckter, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:37 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0