Creating a circular movement

Page 1/2
| 2

By Daemos

Paragon (2044)

Daemos's picture

02-01-2014, 19:34

I have tried about everything and I simply can't get a smooth circular movement.

Here is the deal: I have a centrepoint and an sprite which is 80 pixels away from the centrepoint. The sprite needs to do a circular movement around the centrepoint. Now I have something that works but it just doesn't look very smooth. I have basicly made a circle in excel and created a XY movement table out of the results but it just doesn't look the way it should. Its a little bit choppy in game.

Anyone having usefull ideas on how to create a smooth circle for this?

Login or register to post comments

By ARTRAG

Enlighted (6923)

ARTRAG's picture

02-01-2014, 19:51

How many entries has your table ?
In how many steps do you want a full circle?

By Daemos

Paragon (2044)

Daemos's picture

02-01-2014, 20:27

I currently have 500 steps (500 table entries). 4*125. it works but it doesn't look nice.

Too many steps will use up so much ROM space but it should not be too much of a problem though.

By Marq

Champion (387)

Marq's picture

02-01-2014, 20:58

If the movement is so slow (500 steps at 50 Hz I guess?) and pixel-based it's bound to be a bit choppy. One thing to check is whether you've correctly rounded the coordinates, not just truncated the decimals.

By ARTRAG

Enlighted (6923)

ARTRAG's picture

02-01-2014, 21:06

This is the best circle code I've found.
You have to change PSet with sprite settings and explode the 8 calls in 8 separate routines to cover all octants
NB: it works in ram only but adapting it to rom is very simple


_fcircle:
	
;FastCircle
;
;Author:  Sean McLaughlin (<a href="mailto:sigma_zk@yahoo.com">sigma_zk@yahoo.com</a>)
;Date:    01/18/04 (mm/dd/yy)
;Calc:    TI-83
;Notes:   Uses SMC
;
;D = center_x
;E = center_y
;C = radius

	LD	A,C				;
	OR	A				;
	RET	Z				;
	RET	M				; radius has to be in 0-127
	LD	(_x),DE			;
	LD	B,0				; B = y
	LD	H,B				;
	LD	L,C				;
	ADD	HL,HL			;
	LD	DE,3			;
	EX	DE,HL			;
	SBC	HL,DE			;
	DI					;
_loop:					;
_x 	equ $+1				;
	LD	DE,0			; DE = (xc,yc)
	LD	A,C				; A = x
	CP	B				; nc if (x <= y)
	RET	C				;
;PSet(xc + x, yc + y)	;
	LD	A,E				; A = yc
	ADD	A,B				; A = yc + y
	LD	E,A				; E = yc + y
	EX	AF,AF'			;
	LD	A,D				; A = xc
	ADD	A,C				; A = xc + x
	LD	D,A				; DE = (xc+x,yc+y)
	CALL	PSet		;
;PSet(xc + x, yc - y)	;
	LD	A,E				; A = yc + y
	SUB	B				; A = yc
	SUB	B				; A = yc - y
	LD	E,A				; DE = (xc+x,yc-y)
	CALL	PSet		;
;PSet(xc - x, yc - y)	;
	LD	A,D				; A = xc + x
	SUB	C				; A = xc
	SUB	C				; A = xc - x
	LD	D,A				; DE = (xc-x,yc-y)
	CALL	PSet		;
;PSet(xc - x, yc + y)	;
	EX	AF,AF'			;
	LD	E,A				; DE = (xc-x,yc+y)
	CALL	PSet		;
;PSet(xc + y, yc + x)	;
	LD	A,E				; A = yc + y
	SUB	B				; A = yc
	ADD	A,C				; A = yc + x
	LD	E,A				; E = yc + x
	LD	A,D				; A = xc - x
	ADD	A,C				; A = xc
	ADD	A,B				; A = xc + y
	LD	D,A				; DE = (xc+y,yc+x)
	PUSH	AF			;			[XC+Y]
	CALL	PSet		;
;PSet(xc - y, yc + x)	;
	LD	A,D				; A = xc + y
	SUB	B				; A = xc
	SUB	B				; A = xc - y
	LD	D,A				; DE = (xc-y,yc+x)
	CALL	PSet		;
;PSet(xc - y, yc - x)	;
	LD	A,E				; A = yc + x
	SUB	C				; A = yc
	SUB	C				; A = yc - x
	LD	E,A				; DE = (xc-y,yc-x)
	CALL	PSet		;
;PSet(xc + y, yc - x)	;
	POP	AF				; A = xc + y		-
	LD	D,A				; DE = (xc+y,yc-x)
	CALL	PSet		;

;if(d < 0) err += y << 2 + 6
	BIT	7,H				;
	JR	Z,_PosError
	EX	DE,HL			;
	LD	H,0				;
	LD	L,B				;
	ADD	HL,HL			;
	ADD	HL,HL
	ADD	HL,DE
	LD	DE,6
	ADD	HL,DE
	INC	B
	JP	_loop

_PosError:
;err += (y - x) << 2 + 10: y--
	LD	DE,10
	ADD	HL,DE
	DEC	D
	LD	A,B
	SUB	C
	ADD	A,A
	RL	D
	ADD	A,A
	RL	D
	LD	E,A
	ADD	HL,DE
	INC	B
	DEC	C
	JP	_loop


PSet:

	; ld	e,(_x)
	; ld	c,(_y)
	; call	_setpixel

	push	de
	push	bc
	push	hl		; DE = (x,y)
	ld		c,e		; y -> c
	ld		e,d		; x -> e
	call	_setpixel
	pop		hl
	pop		bc
	pop		de
	ret

By Daemos

Paragon (2044)

Daemos's picture

02-01-2014, 21:16

.... wow it looks quite amazing

Thanks I will give it a try and see what happens.

By Manuel

Ascended (19273)

Manuel's picture

02-01-2014, 22:22

What, where's the sin and cos stuff? Tongue

By hap

Paragon (2042)

hap's picture

02-01-2014, 22:30

Maybe it's Bresenham circle.
BTW: if you're using a lookup table, you only need 45 degrees, the rest is easy to calculate from that.

By ARTRAG

Enlighted (6923)

ARTRAG's picture

02-01-2014, 23:10

It is a Bresenham algorithm and it covers only one octant (0-45°).
The other octants are covered by mirroring/reflections, this is why if you want a single sprite rotating in circle, you need to replicate the algorithm 8 times, one for octant, and call each branch in the appropriate order.

Moreover, each iteration moves the sprite/point of one pixel on the x or y direction (and less than one pixel on the y or x direction), so the sole way to change the rotation speed is by calling the subroutine twice or more times per frame

Anyway it will look absolutely and totally smooth, without storing tables

By Daemos

Paragon (2044)

Daemos's picture

03-01-2014, 12:39

I however need to move the sprite 1 pixel per frame. I also need to be able to variate the speed. I am not sure if I can pull it of with this routine because it makes a full 45 degrees movement per call right?. I can anyhow use it to generate a nice working table.

Is there also a way to apply such algorithm to create a smooth XY coord table in for example excel which I can export?

By ARTRAG

Enlighted (6923)

ARTRAG's picture

03-01-2014, 12:47

Sure! I'll try to "open" the loop tonight and post a function that moves the sprite one pixel per call (or a small table)

Page 1/2
| 2