TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: RAIN CODE on June 28, 2012, 02:07:29 AM

Title: how to check balls out of view
Post by: RAIN CODE on June 28, 2012, 02:07:29 AM
hi again, guys

Do you know how to determine if a block moving around the screen is out of the current view - let say the block move from the screen centre to 9 o'clock (left hand side) then disappear out of view. Is there any shortest way to determine if the block is out of view ?? with the use of lisp or setvar or limits ??

I am trying to add a boss level to the game. The boss will shoot out balls at pacman. But how to determine if the balls are out of the current view ? if the balls are out of view then I will discard those balls to speed up the game.

I could write to check if balls is within screen view by using -
      if balls insertion pt x is > bottom most left corner x AND insertion pt x is < top most right corner x ... and do the same for y axis
but it is too messy and long and it will very likely slow down the game since about 10 -15 small balls flying around the screen.

Thanks

Title: Re: how to check balls out of view
Post by: Kerry on June 28, 2012, 03:09:03 AM
I'd usE VSMAX and VSMIN.

The X and Y of each will give you the edges of the viewport.
Then the INBOUNDS is just an OR check on the edges ...
if
  or
    ball X < left
    ball X > right
    ball Y < top
    ball Y < btm
  then the ball is out of bounds.

The OR will return True at the first test that passes, so it should be reasonably fast.

You may need to refresh the variables holding the edge values if you are allowing the viewport to  resize.


added:
I'd hold the edges value in the application scope and use a method that has the ballX and ballY as parameters.
.... that way you can use the same method to check all balls.

Regards
kdub
Title: Re: how to check balls out of view
Post by: Stefan on June 28, 2012, 03:19:48 AM
This will return T if a point (pb) is out of visible screen.
Code: [Select]
(defun is_out (pb / cen scr h w)
  (setq cen (getvar 'viewctr)
        scr (getvar 'screensize)
        h (* 0.5 (getvar 'viewsize))
        w (/ (* (car scr) h) (cadr scr))
        )
  (vl-some '(lambda (a b c) (not (< a b c))) (mapcar '- cen (list w h)) pb (mapcar '+ cen (list w h)))
  )
Edit: switched w and h in (vl-some...
Title: Re: how to check balls out of view
Post by: Tharwat on June 28, 2012, 03:48:02 AM
My version with grread function and it is considered just as an example ...  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ p1 p2 gr)
  2. ;;;;; Tharwat 28. June. 2012 ;;;;;
  3.   (if (and (setq p1 (getpoint "\n Specify point :"))
  4.            (setq p2 (getcorner p1 "\n Corner :"))
  5.       )
  6.     (while (eq (car (setq gr (grread t 15 1))) 5) (redraw)
  7.       (foreach n '((0.5 0.5 0.0) (-0.5 0.5 0.0)(-0.5 -0.5 0.0)(0.5 -0.5 0.0))
  8.         (grdraw
  9.           (cadr gr)
  10.           (mapcar '+ (mapcar (function (lambda (x) (* x (/ (getvar "VIEWSIZE") 25.))))
  11.               (trans n 1 0))
  12.             (cadr gr)
  13.           )
  14.           1
  15.           0
  16.         )
  17.       )
  18.  
  19.       (if
  20.         (and
  21.           (< (car p1) (car (cadr gr)))
  22.           (> (car p2) (car (cadr gr)))
  23.           (> (cadr p1) (cadr (cadr gr)))
  24.           (< (cadr p2) (cadr (cadr gr)))
  25.         )
  26.          (princ "\n inside ...... ")
  27.          (princ "\n outside ..... ")
  28.       )
  29.     )
  30.   )
  31.   (redraw)
  32.   (princ)
  33. )
  34.  
Title: Re: how to check balls out of view
Post by: Lee Mac on June 28, 2012, 07:14:45 AM
A variation of Stefan's:

Code - Auto/Visual Lisp: [Select]
  1. (defun onscreen-p ( p / c h v )
  2.     (setq c (getvar 'viewctr)
  3.           h (/ (getvar 'viewsize) 2.0)
  4.           v (list (* h (apply '/ (getvar 'screensize))) h)
  5.     )
  6.     (apply 'and (mapcar '< (mapcar '- c v) p (mapcar '+ c v)))
  7. )
Title: Re: how to check balls out of view
Post by: RAIN CODE on June 29, 2012, 01:32:21 AM
You guys are amazing. Surely you Lisp is much shorter than mine.
I am really impressed. I really don't understand much about mapcar and lambda.

Could anyone explains how your program works. I want to learn too. Please dont direct me to other web site for info.
I tried downloading lessons for mapcar and lambda but I could not grasp how it actually work.

Explaining here is better than telling me to read elsewhere.

Thanks.

Title: Re: how to check balls out of view
Post by: BlackBox on June 29, 2012, 09:19:08 AM
This Autodesk University course in particular helped me greatly when I first learned of functions such as Apply, Mapcar, and Lambda:

LISP: Advance Yourself Beyond a Casual Programmer (http://au.autodesk.com/?nd=class&session_id=5361)

Direct link to course handout (http://aucache.autodesk.com/au2009/sessions/5361/CP304-1-Handout-.pdf)... Other resources available when logged in at the link above.

HTH
Title: Re: how to check balls out of view
Post by: CAB on June 29, 2012, 10:08:46 AM
Some info from my index:

----------  Mapcar  Lambda  Apply   ------------
http://www.theswamp.org/index.php?topic=2953.0   (MP)
http://www.theswamp.org/index.php?topic=340.0   (CAB)
http://www.cadtutor.net/forum/showthread.php?52127-Mapcar-lambda-Description
http://lee-mac.com/mapcarlambda.html


Find here links and more. Scroll down for several post.
http://www.theswamp.org/index.php?topic=24700.msg359343#msg359343
Title: Re: how to check balls out of view
Post by: Matt__W on June 29, 2012, 11:13:56 AM
For what it's worth, I never let my balls out of my sight.  #outofcontext
Title: Re: how to check balls out of view
Post by: Kerry on June 29, 2012, 11:22:09 AM

Thanks Matt, You just caused me to win $20.00.
Title: Re: how to check balls out of view
Post by: RAIN CODE on July 05, 2012, 01:39:55 AM

Thanks guy for the mapcar and lambda lessons or tutorial.

I need more sleep before I read that.
Have been programming into the wee hours when people are sleeping and dreaming.
So much so that my brain is telling me I need to stop programming for a while.  :blank:

something like a writer block, it refuse to think.  :blank:

Thanks again